public class B { public static B t1 = new B(); public static B t2 = new B(); { System.out.println("构造块"); } static { System.out.println("静态块"); } public static void main(String[] args) { B t = new B(); } }
public class B { public static B t1 = new B(); public static B t2 = new B(); { System.out.println("构造块"); } static { System.out.println("静态块"); } public static void main(String[] args) { B t = new B(); } }
静态块 构造块 构造块 构造块
构造块 静态块 构造块 构造块
构造块 构造块 静态块 构造块
构造块 构造块 构造块 静态块
public static B t1 = new B(); public static B t2 = new B(); 所以会打印两个“构造块” 接着在执行静态代码块 打印“静态块”, 最后执行B t = new B(); 打印“构造块”