简体   繁体   English

java中如何在不调用构造函数的情况下创建实例

[英]How instance is created without calling constructor in java

I read on Deserializing java object and came to know that,我阅读了反序列化 java 对象并了解到,

While deserializing java object, If instance implements the serializable interface, then an instance of the class is created without invoking it's constructor.在反序列化 java 对象时,如果实例实现了可序列化接口,则创建类的实例而不调用它的构造函数。

By going through this link , I came to know Java uses sun.reflect.ReflectionFactory newConstructorForSerialization() method for creating new instance of Class without calling constructor.通过浏览此链接,我了解到 Java 使用 sun.reflect.ReflectionFactory newConstructorForSerialization() 方法来创建 Class 的新实例,而无需调用构造函数。

Is it creates totally new constructor for deserialization process?它是否为反序列化过程创建了全新的构造函数?

I want to know how object is created without calling its constructor.我想知道如何在不调用其构造函数的情况下创建对象。 How this method actually works, can someone please explain in simple words.这种方法实际上是如何工作的,有人可以用简单的话解释一下。

In Java bytecode the constructor is actually a method you call on an already created object.在 Java 字节码中,构造函数实际上是您在已经创建的对象上调用的方法。 So if you're writing bytecode you can simply skip the second step.因此,如果您正在编写字节码,则可以简单地跳过第二步。

In fact, there are a lot of control flow constructs that you can express in bytecode but that you can't write in Java source code.事实上,有很多控制流结构可以用字节码表达,但不能用 Java 源代码编写。

Internally, Java serialization is built into the JVM so it can bypass any rules it likes.在内部,Java 序列化内置于 JVM 中,因此它可以绕过它喜欢的任何规则。

This just underscores the difference between Java the programming language (where new always invokes the constructor) and Java the platform (where NEW is an opcode that pushes an unconstructed instance of the desired type onto the stack).这只是强调了 Java 编程语言(其中new总是调用构造函数)和 Java 平台(其中NEW是将所需类型的未构造实例推送到堆栈上的操作码)之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以创建类的实例进行测试而无需调用构造函数? - Can an instance of a class be created for testing without calling the constructor? 是否可以在不调用构造函数的情况下在Java中创建对象的实例? - Is it possible to create an instance of an object in Java without calling the constructor? 创建对象实例而不调用超类构造函数 - Creating instance of object without calling superclass constructor 在不知道类名称的情况下调用构造函数(java) - Calling the constructor without knowing the name of the class (java) 通过Java中的构造函数调用没有实例的方法 - Invoke method without instance via constructor in Java 在没有对象的情况下调用自己创建的Java函数? - Calling own created java functions without an object? 为什么我无法访问在构造函数中创建的 class 实例 ( Java ) - Why can I not access an instance of class that is created in a constructor ( Java ) Java 构造函数调用和更改父 class 实例变量的值 - Java constructor calling and changing values of parent class instance variables 为什么以及如何在不由Java构造函数初始化的情况下初始化实例变量? - why and how instance variable get initialized without getting initialized by constructor in Java? 调用不带参数的构造函数 - Calling a Constructor without parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM