执行顺序:
静态代码块 > 非静态代码块 > 构造方法
示例:
public class Test {
{
System.out.println("not static run");
}
static {
System.out.println("static run");
}
public Test() {
System.out.println("constructor run");
}
}
public class TestString {
public static void main(String[] args) {
Test test1 = new Test();
Test test2 = new Test();
Test test3 = new Test();
}
}
输出结果:
static run
not static run
constructor run
not static run
constructor run
not static run
constructor run