package test;
//初始化顺序:
静态:
变量
代码块
构造函数
普通
变量
代码块
构造函数
public static int k=0;
public static TestVar t1=new TestVar("t1"); /*第1步 该步其实是在创建对象执行构造函数 在new这个对象之
前必须还要先执行变量和代码块的初始化,所以要先执行print(j),然后在执行普通代码块构造*/
public static TestVar t2=new TestVar("t2"); //2
public static int i=print("i"); //3
public static int n=99; //4
public int j=print("j"); //6
{
print("构造"); //7
}
static {
print("静态"); //5
}
public TestVar(String str) {
System.out.println((++k)+":"+str+" i="+i+" n="+n);
}
public static int print(String str) {
System.out.println((++k)+":"+str+" i="+i+" n="+n);
++n;
return ++i;
}
}
public class Test2 {
public static void main(String[] args) {
TestVar t1=new TestVar("init");
}
}
==============
1:j i=0 n=0
2:构造 i=1 n=1
3:t1 i=2 n=2
4:j i=2 n=2
5:构造 i=3 n=3
6:t2 i=4 n=4
7:i i=4 n=4
8:静态 i=5 n=99
9:j i=6 n=100
10:构造 i=7 n=101
11:init i=8 n=102