何时初始化实例变量并分配值? [英] When do instance variables get initialized and values assigned?

查看:106
本文介绍了何时初始化实例变量并分配值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当doees实例变量初始化时?是在构造函数块完成之后还是之前?

When doees the instance variable get initialized? Is it after the constructor block is done or before it?

考虑这个例子:

public abstract class Parent {

 public Parent(){
   System.out.println("Parent Constructor");
   init();
 }

 public void init(){
   System.out.println("parent Init()");
 }
}







public class Child extends Parent {

private Integer attribute1;
private Integer attribute2 = null;

public Child(){
    super();
    System.out.println("Child Constructor");
}

public void init(){
    System.out.println("Child init()");
    super.init();
    attribute1 = new Integer(100);
    attribute2 = new Integer(200);
}

public void print(){
    System.out.println("attribute 1 : " +attribute1);
    System.out.println("attribute 2 : " +attribute2);
}
}







public class Tester {

public static void main(String[] args) {
    Parent c = new Child();
    ((Child)c).print();

}
}






OUTPUT:



父构造函数


OUTPUT:

Parent Constructor

子init()

父Init()

子构造函数

属性1:100

属性2:null


  1. 当属性1和1的内存时2在堆中分配?

  1. When the memory for the atribute 1 & 2 are allocated in the heap ?

很想知道为什么属性2为NULL?

Curious to know why is attribute 2 is NULL ?

是否有任何设计缺陷?


推荐答案


当属性1和1的内存时2在堆中分配?

When the memory for the atribute 1 & 2 are allocated in the heap ?

new <时,整个对象的内存被分配在输入 java.lang.Object 构造函数之前调用/ code>运算符。内存是为 init 中的单个 Integer 实例分配的,但是为各个属性分配内存没有意义 - 只有整个对象。

The memory for the object as a whole is allocated when the new operator is invoked, before the java.lang.Object constructor is entered. Memory is allocated for individual Integer instances in init, but there is no point when memory is allocated for individual properties -- only whole objects.


很想知道为什么属性2为NULL?

Curious to know why is attribute 2 is NULL ?

在超级构造函数中调用 init 方法,因此 attribute2 被分配 new Integer(200),然后调用子类构造函数,它按照它们在源代码中出现的顺序应用属性初始值设定项。这一行

The init method is called in the super constructor, so attribute2 is assigned new Integer(200), and then the subclass constructor is invoked which applies property initializers in the order they appear in the source code. This line

private Integer attribute2 = null;

覆盖 init()指定的值至 null

如果您添加了对

 System.out.println("attribute 2 : " +attribute2);

在您致电 super(); 然后这将变得明显。

right after your call to super(); then this will become apparent.


是否有任何设计缺陷?

Are there any design flaws?

在基类完成初始化之前调用子类方法是危险的。子类可能依靠其基类的不变量,以保护自己的不变量,如果基类的构造函数尚未完成,那么它的不变量可能不成立。

Calling sub-class methods before the base class has finished initializing is dangerous. The sub-class might rely on its base-class's invariants to protect its own invariants, and if the base-class constructor has not completed, then its invariants may not hold.

这也可能混淆C ++程序员,谁预料到的init A调用基类调用基类的版本,因为C ++重写虚表指针的构造等等输入。

This is also likely to confuse C++ programmers and the like who would expect a call to init from the base class to invoke the base class's version since C++ rewrites the vtable pointer as constructors are entered.

参见 Java语言规范

这篇关于何时初始化实例变量并分配值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆