public class House { Window w1=new Window(1); public House() { // TODO Auto-generated constructor stub print("Hourse()"); w3=new Window(33); } Window w2=new Window(2); public void f() { print("execute f() method"); } Window w3=new Window(3); } public class Window { public Window(int marker) { // TODO Auto-generated constructor stub print("clinit window("+marker+")"); } } public class OrderOfinitialization { public static void main(String[] args) { House house=new House(); house.f(); } }
clinit window(1) clinit window(2) clinit window(3) Hourse() clinit window(33) execute f() method
有几点说明
1.变量定义优先任何方法调用(包括构造器)
2.static定义的变量优先执行
3.如果一个类有父类关系,那父类的构造优先执行