听了好久的类加载执行顺序,自己一直没有实践过,今天休息时想到这个问题,亲自动手验证如下:
上代码
public class Domain { public Domain(String str){ System.out.println(str); }}public class SuperClass { private static Domain d1 = new Domain("father static member variable"); private Domain d2 = new Domain("father common member variable"); static{ System.out.println("father static code block"); } { System.out.println("father dynamic code block"); } SuperClass(){ System.out.println("father no arguments constructor"); }}public class SubClass extends SuperClass { private static Domain d1 = new Domain("child static member variable"); private Domain d2 = new Domain("child common member variable"); static { System.out.println("child static code block"); } { System.out.println("child dynamic code block"); } public SubClass(){ System.out.println("child no arguments constructor"); } public static void main(String[] args){ SubClass t1 = new SubClass(); }}
以上三个类,SubClass继承自SuperClass,Domain为测试用的类。执行结果如下: