类的初始化顺序:
- 静态初始化块
- main函数中的输出(调用构造函数前)
- 实例的初始化块(初始化匿名内部类的地方)
- 构造函数初始化
import static java.lang.System.out;
public class InitializerDemo {
public InitializerDemo() {
out.println("in constructor"); // Fourth
}
static {
out.println("in static initializer"); // First
}
{
out.println("in instance initializer"); // Third
}
public static void main(String[] args) {
out.println("in main() method"); //second
new InitializerDemo();
}
}