Java中含有静态成员的的初始化顺序
class Bowl{ Bowl(int marker){ System.out.println("Bowl(" + marker + ")" ); } void f1(int marker){ System.out.println("f1(" + marker + ")"); } } class Table{ //首先是静态变量 static Bowl bow1 = new Bowl(1);//2 Table(){ System.out.println("Table()");//4 bowl2.f1(1);//5 } void f2(int marker){ System.out.println("f2(" + marker + ")"); } static Bowl bowl2 = new Bowl(2);//3 } class CupBoard{ Bowl bowl3 = new Bowl(3);//8 不是static变量,会与每一个对象绑定,所以每创建一次对象都会初始化一次 static Bowl bowl4 = new Bowl(4);//6 CupBoard(){ System.out.println("CupBoard");//9 bowl4.f1(2);//10 } static Bowl bowl5 = new Bowl(5);//7 void f3(int marker){ System.out.println("f3(" + marker + ")"); } } public class Main { //首先初始化静态变量 public static void main(String[] args) { System.out.println("Creating new CupBoard in main"); new CupBoard();//11 System.out.println("Creating new CupBoard in main"); new CupBoard();//12 table.f2(1);//13 cupBoard.f3(1);//14 } static Table table = new Table();//1 static CupBoard cupBoard = new CupBoard();//5 } //顺序: //首先是静态对象 //非静态对象 //构造函数