在main方法里创建一个Test2的对象就可以执行程序
/**
* static初始化学习
* @author
* @version 1.0
*/
public class StaticTest {
/*
* 对象创建时,static成员首先进行初始化,然后是非static成员,最后调用构造器
* */
Test2 t = new Test2();
}
class Test1 {
public Test1(int seq) {
System.out.println("初始化变量" + seq);
}
}
class Test2 {
static Test1 t1 = new Test1(1);
public Test1 t2 = new Test1(2);
public Test2() {
System.out.println("调用构造器");
}
static Test1 t3;
static {
t3 = new Test1(3);
}
}
执行结果