public class ClassA { public ClassA(){ System.out.println("A 构造..."); } { System.out.println("this is A...."); } static{ System.out.println("A static..."); } }
public class ClassB extends ClassA{ public ClassB(){ System.out.println("B 构造..."); } { System.out.println("this is B...."); } static{ System.out.println("B static..."); } public static void main(String[] args) { new ClassB(); } }
运行结果:
A static... B static... this is A.... A 构造... this is B.... B 构造...
结论:
对于静态变量、静态初始化块、变量、初始化块、构造器
它们的初始化顺序依次是:
(静态变量、静态初始化块)>(变量、初始化块)>构造器