JAVA--初始化顺序
代码及测试结果如下:
public class InitSequenceTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubnew BB();}}class AA {private int i = 100;protected int j;AA(){System.out.println("i="+i +",j="+j);j=999;}static int x = initStaticData("init x value");protected static int initStaticData(String s){System.out.println(s);return 222;}private int yy = initStaticData("init yy value");}class BB extends AA {private static int jj = initStaticData("init JJ value");BB(){System.out.println("jj="+jj);System.out.println("j="+j);}private int y = initStaticData("init y value");}
result:
init x valueinit JJ valueinit yy valuei=100,j=0init y valuejj=222j=999
??结果分析: