[英]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: 它的基本内容是:
null
or 0) 分配了内存空间(所有字段均具有相关类型的默认值,例如null
或0) java.lang.Object
最终,链到达java.lang.Object
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: 但是,构造函数每一位的计时很重要:
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.) (如果name
是final
,则在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.