书中原文:在类的内部,变量定义的先后顺序决定了初始化的顺序。即使变量定义散布于方法定义之间,它们仍旧会在任何方法(包括构造器)被调用之前得到初始化
public class Test01 {
public static void main(String[] args) {
House house = new House();
}
}
class Window {
public Window(int marker) {
System.out.println("Window:" + marker);
}
}
class House {
Window w1 = new Window(1);
public House() {
System.out.println("House()");
}
Window w2 = new Window(2);
}
执行结果:
Window:1
Window:2
House()