整体顺序是static members,instance variables,constructor
而static members之间(包括静态变量,静态初始化块),instance variables之间都是按textual order(即书写代码时顺序)排序的
public class Test {
static {
printInit("static block initialized");
}
private static int j = printInit("Test.j initialized");
private int k = printInit("Test.k initialized");
int l;
private static void hello() {
System.out.println("static hello");
}
public Test() {
System.out.println("k = " + k);
System.out.println("l = " + l);
}
{
printInit("nonstatic block initialized");
}
private static int x2 = printInit("static Test.x2 initialized");
static int printInit(String s) {
System.out.println(s);
return 47;
}
public static void main(String[] args) {
System.out.println("Test constructor");
new Test();
}
}
结果:
static block initialized
Test.j initialized
static Test.x2 initialized
Test constructor
Test.k initialized
nonstatic block initialized
k = 47
l = 0