使用Eclipse,JCK1.8实验,实验代码如下。
public class ClassInitTest{ PRivate static int a=2; public ClassInitTest() { System.out.println("ClassInitTest constructor:" + System.currentTimeMillis() + ": class name-" + getClass().getName()); } private int b=4; public static void main(String[] args) { System.out.println("ClassInitTest main 1:" + System.currentTimeMillis() + ": a-" + a); Test test1 = new Test(); System.out.println("ClassInitTest main 2:" + System.currentTimeMillis() + ": ClassInitTest: test1.t5-" + test1.t5); System.out.println("ClassInitTest main 3:" + System.currentTimeMillis() + ": ClassInitTest: test1.t7-" + test1.t7); }}class Test{ private int t1 = 7; private static int t2 = 4; private int t3 = 7; private static Test test2 = new Test();// private Test test3 = new Test(); private int t4 = 5; public static int t5 = 3; private int t6 = 6; { int counter = 3; System.out.println("Test initial block 1st: " + System.currentTimeMillis() + ": t2-" + t2 + ", t5-"+ t5 + ", t7-" + t7 + ", t4-" + t4 + ", t6-" + t6 +", counter1-" + counter); counter ++; } public static int t7 = 4; public int t8 = 5; public static int t9 = 4; { System.out.println("Test initial block 2nd:: " + System.currentTimeMillis() + ": t7-" + t7 + ", t4-" + t4 + ", t8-" + t8); } public Test() { System.out.println("Test Constructor 1: " + System.currentTimeMillis() + ": t2 " + t2 + ", t5: "+ t5 + ", t7: " + t7); int counter = 0; ++t5; System.out.println("Test Constructor 2: " + System.currentTimeMillis() + ": t2 " + t2 + ", t5: "+ t5 + ", t7: " + t7 + ", counter2: " + (++counter)); ++t7; System.out.println("Test Constructor 3: " + System.currentTimeMillis() + ": counter2: " + (++counter)); System.out.println("Test Constructor 4: " + System.currentTimeMillis() + ": t2 " + t2 + ", t5: "+ t5 + ", t7: " + t7 + ": counter2: " + (++counter)); }}打印结果如下:
ClassInitTest main 1:1487332792727:a-2
Test initial block 1st:1487332863609: t2-4, t5-0, t7-0, t4-5, t6-6, counter1-3
Test initial block 2nd::1487332880470: t7-0, t4-5, t8-5
Test Constructor 1: 1487332886302:t2 4, t5: 0, t7: 0
Test Constructor 2: 1487332887782:t2 4, t5: 1, t7: 0, counter2: 1
Test Constructor 3: 1487332888490:counter2: 2
Test Constructor 4: 1487332888902:t2 4, t5: 1, t7: 1: counter2: 3
Test initial block 1st:1487333194886: t2-4, t5-3, t7-4, t4-5, t6-6, counter1-3
Test initial block 2nd::1487333197646: t7-4, t4-5, t8-5
Test Constructor 1: 1487333198990:t2 4, t5: 3, t7: 4
Test Constructor 2: 1487333200494:t2 4, t5: 4, t7: 4, counter2: 1
Test Constructor 3: 1487333201654:counter2: 2
Test Constructor 4: 1487333202102:t2 4, t5: 4, t7: 5: counter2: 3
ClassInitTest main 2:1487333254326:ClassInitTest: test1.t5-4
ClassInitTest main 3:1487333255094:ClassInitTest: test1.t7-5
有些变量无法打印,所以最好的是利用我的代码,设好断点,单步调试,观察变量值。
结论如下:
java类执行的流程:所有静态变量赋值为默认初始化值→从类头部第一个静态代码块开始顺序执行所有静态代码块(静态变量的声明也可看做是一个静态代码块)→非费静态成员变量声明语句或初始化代码块,二者按顺序谁在前谁先执行→构造方法→构造方法之后的其他静态代码。
在构造时的类中语句执行顺序:从类头部的第一个非费静态成员变量声明语句或初始化代码块开始执行→顺序执行静态变量声明语句→顺序执行构造方法中的语句。如果静态变量之前已经初始化,则直接跳到构造方法执行。
鸣谢:
感谢这两位作者的文章。
http://blog.csdn.net/u010444251/article/details/53107165
http://www.cnblogs.com/octobershiner/archive/2012/03/12/2391899.html
新闻热点
疑难解答