class nonStatic1 {
nonStatic2 n2 = new nonStatic2();
nonStatic1() {
this.print();
}
void print() {
System.out.println("insidenonStatic1");
}
}
class nonStatic2 {
nonStatic2() {
this.print();
}
void print() {
System.out.println("insidenonStatic2");
}
}
class nonStatic3 {
nonStatic1 n1 = new nonStatic1();
nonStatic3() {
this.print();
}
void print() {
System.out.println("insidenonStatic3");
}
}
public class NonStaticInitializationTest {
nonStatic2 n2 = new nonStatic2(); // <-- Never initialized!!
public static void main(String args[]) {
System.out.println("initialising n3 through main");
nonStatic3 n3 = new nonStatic3();
}
}
答案 0 :(得分:0)
它没有初始化,因为它是NonStaticInitializationTest
的实例成员,这意味着您必须为该类创建一个实例,以便该成员存在。
创建该实例后,将立即创建nonStatic2
实例,但是目前,您仅在main方法中工作,该方法是静态成员,并且不需要其类的实例。