在类的内部,变量定义的先后顺序决
定了初始化的顺序。即使变量定义于方法定义之间,他们仍旧会在任何方法(包括构造方法)调用之前得到初始化。
/** * Created by wangw on 2016/2/28. */ public class Demo2 { public static void main(String[] args){ House house = new House(); house.f(); } } class Window{ Window(int marker){ System.out.println("window-==="+marker); } } class House{ Window w1 = new Window(1); House(){ System.out.println("House()"); w3 = new Window(33); } Window w2 = new Window(2); void f(){ System.out.println("f"); } Window w3 = new Window(3); }
输出:
window-===1 window-===2 window-===3 House() window-===33 f