代码及测试结果如下:
public class InitSequenceTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new 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 value init JJ value init yy value i=100,j=0 init y value jj=222 j=999
??结果分析: