听了好久的类加载执行顺序,自己一直没有实践过,今天休息时想到这个问题,亲自动手验证如下:

上代码

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为测试用的类。执行结果如下:

类加载执行顺序

 

相关文章:

  • 2021-06-25
  • 2022-12-23
  • 2022-01-23
  • 2021-04-28
  • 2022-12-23
  • 2021-11-20
  • 2021-10-19
  • 2021-09-14
猜你喜欢
  • 2021-08-10
  • 2021-07-08
  • 2021-07-22
  • 2022-12-23
  • 2022-02-05
  • 2021-07-26
  • 2022-01-27
相关资源
相似解决方案