How JVM loads parent classes in Java

后端 未结 3 1904
醉话见心
醉话见心 2020-12-18 06:28

Code:

class A {
    static {
        System.out.println(\"loading A static 1\");
    }
    static {
        System.out.println(\"loading A static 2 B.c= \"+B         


        
3条回答
  •  旧时难觅i
    2020-12-18 06:44

    You can check this by executing it with java -verbose Test:

    ...
    [Loaded A from file:/.../src/main/java/]
    [Loaded B from file:/.../src/main/java/]
    loading A static 1
    loading A static 2 B.c= 0
    loading static 3
    loading B A.a= 10
    ...
    

    So no, the parent class is loaded first too.

提交回复
热议问题