简体   繁体   English

当我们创建一个对象时,对象的创建和构造函数的执行是否同时发生?

[英]when we create an object ,do creation of object and the execution of constructor happens at the same time?

When we create an object, do creation of object and the execution of constructor happens at the same time or first object is created then constructor execution take place? 当我们创建一个对象时,对象的创建和构造函数的执行是同时发生还是创建第一个对象然后执行构造函数?

Its written in the Herbert Schildt: "Once defined ,the constructor is automatically called immediately after the object is created ,before the new operator completes". 其用赫伯特·希尔德(Herbert Schildt)写道:“一旦定义,在创建对象之后, 操作符完成之前,构造函数将立即自动调用”。 My queue is that if new operator has not finished with it's memory allocation then how can be constructor be called before new gets completed as it's written constructor is called only after object is created. 我的队列是,如果new运算符尚未完成其内存分配,那么如何在new完成之前调用构造函数,因为只有在创建对象之后才调用new编写的构造函数。

Section 12.5 of the JLS gives the details. JLS的12.5节提供了详细信息。 The bare bones of it is: 它的基本内容是:

  • Memory space is allocated (with all fields having the default value for the relevant type, eg null or 0) 分配了内存空间(所有字段均具有相关类型的默认值,例如null或0)
  • The specified class's constructor is called, which will immediately chain to either another constructor in the same class or a superconstructor 指定的类的构造函数被调用,它将立即链接到同一类中的另一个构造函数或超构造函数
  • Eventually the chain reaches java.lang.Object 最终,链到达java.lang.Object
  • In each constructor chain coming back down, instance variables are initialized based on their field initializers, if any (and only if we didn't chain to another constructor in the same class), then the constructor body code executed. 在返回的每个构造函数链中,实例变量都是根据其字段初始化器(如果有的话)进行初始化的(并且仅当我们未链接到同一类中的另一个构造函数时),然后构造函数的主体代码被执行。 That then returns to the calling constructor, etc. 然后返回到调用构造函数,依此类推。

The JLS goes into more detail, of course, including cases where there isn't enough memory or a constructor body throws an exception. 当然,JLS会更详细,包括内存不足或构造函数主体引发异常的情况。

The timing of each bit of the constructor is important though: 但是,构造函数每一位的计时很重要:

  • Chain to this/super constructor 链接到此/超级构造函数
  • (Implicit) Assign initial values to fields if the chain was to a superclass constructor (隐式)如果链是超类构造函数,则将初始值分配给字段
  • Constructor body 构造体

It's important to understand that if a superconstructor invokes an overridden method which reveals the value of a field, it will not have gone through the field initializer yet. 重要的是要了解,如果超级构造函数调用一个覆盖方法来显示字段的值,那么它将尚未通过字段初始化程序。 So you can see the default value of the field rather than the value you'd expect to from the initializer. 因此,您可以看到该字段的默认值,而不是初始化程序期望的值。 For example: 例如:

class Bar extends Foo {
    private String name = "fred";

    @Override public String toString() {
        return name;
    }
}

If the Foo constructor calls toString() , that will null rather than "fred" . 如果Foo构造函数调用toString() ,则它将为null而不是"fred"

(If name is final , then it's treated as a constant in toString() and other things happen, but that's a different matter.) (如果namefinal ,则在toString()中将其视为常量,并且发生其他事情,但这是另一回事。)

The new operator creates a new object by performing a few operations in sequence: it allocates memory for the object as needed (this operation might trigger other operations in the JVM), it executes the code defined in the constructor being called and finally, it returns to the caller with a reference to the newly created object. new运算符通过依次执行一些操作来创建新对象:它根据需要为该对象分配内存(此操作可能会触发JVM中的其他操作),它执行被调用的构造函数中定义的代码,最后返回调用者,并引用新创建的对象。 It's worth noting that the constructor call happens after all the variables are default initialized in the current class and that this same sequence happens from the topmost class in the hierarchy down to the current class (so Object() is executed first etc.). 值得注意的是,构造函数调用发生在所有变量在当前类中被默认初始化之后,并且相同的序列发生在层次结构中最顶层的类直到当前类(因此Object()首先执行等)。

So technically the constructor executes on the newly created object, ie, the memory space allocated for it, it could never execute on memory that hasn't been allocated or is being allocated. 因此,从技术上讲,构造函数在新创建的对象(即为其分配的内存空间)上执行,因此它永远不会在尚未分配或正在分配的内存上执行。

When you call new , memory is allocated on the heap for the object, including it's variables which are initialized to default values. 当您调用new ,将在对象的堆上分配内存,包括初始化为默认值的变量。 If values are supplied as constructor parameters, those values are used. 如果将值作为构造函数参数提供,则使用这些值。

Once the constructor is finished with initialization, the variable to which it is assigned is created on the stack, with address of the object in the heap as its value. 一旦构造函数完成初始化,就在堆栈上创建分配给它的变量,并以堆中对象的地址为其值。

I suggest you watch this Stanford lecture where this is explained in just enough details so that anyone can understand it. 我建议您观看本斯坦福大学的讲座 ,在其中进行了足够详细的解释,以便任何人都可以理解。

暂无
暂无

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

相关问题 我们初始化我们的主对象,并在其构造函数中创建一个新对象。 当我们摧毁主要对象时,它的创建会发生什么? - We initialize our main object, and it creates a new object in its constructor. When we destroy the main object, what happens to its creation? 构造函数的执行顺序和对象的创建 - Execution order of constructor and creation of object Java:在创建时将值传递给对象时,应该被初始化的成员会怎样? - Java: What happens to the supposed-to-be initialized members when passing a value to their object at creation time? 我们可以同时使用 new 关键字创建 Bean 和 object 吗? - Can we create Bean and an object using new keyword at the same time? 如果我们创建子类的对象,是否会创建父类的对象? - will there be object creation of parent class if we create an object of child class? 关于对象创建的基本知识:每次我们要为View设置侦听器时,创建一个新的侦听器对象是否是一种好习惯? - Basic things about object creation :is it a good practice to create a new object of listener,every time we want to set a listener for a View 创建新对象时会发生什么? - What happens when you create a new object? 为什么我们使用“ new”运算符创建一个对象,然后将其作为参数传递给构造函数内部? - Why do we create an object with “new” operator and then pass it as a parameter inside the Constructor? 为什么我们不能创建chield class object 到它自己的类型,而parent 的构造函数方法在Java 中创建Object - Why cant we are able to create chield class object to its own type when constructor method of parent is used to create Object in Java 当我们在 JDBC 连接 object 中将 useLegacyDatetimeCode 设置为 false 时会发生什么 - What happens when we set useLegacyDatetimeCode to false in JDBC connection object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM