当我运行下面的代码时:
public class Test {
Test(){
System.out.println("1");
}
{
System.out.println("2");
}
static {
System.out.println("3");
}
public static void main(String args[]) {
new Test();
}
}
我希望按照这个顺序输出:
1
2
3
但我得到的顺序是相反的:
3
2
1
有人能解释一下为什么它会反向输出吗?
================
另外,当我创建多个Test
实例时:
new Test();
new Test();
new Test();
new Test();
静态块只在第一次执行。