What does “When a Class is loaded” actually mean?

牧云@^-^@ 提交于 2019-11-27 14:42:13

问题


It is said that static blocks in java run only once when that class is loaded. But what does it actually mean? At which point is a class loaded by JVM (Java Virtual Machine)?

Is it when the main method in that class is called? And is it that all the super-classes of the same class are also loaded when the main method starts execution?

Consider that A extends B and B extends C. All have static blocks. If A has the main method, then what will be the sequence of execution of static blocks?


回答1:


This is described in the Execution section of the JLS. Namely:

Initialization of a class consists of executing its static initializers and the initializers for static fields declared in the class. Initialization of an interface consists of executing the initializers for fields declared in the interface.
Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized. Similarly, the superinterfaces of an interface need not be initialized before the interface is initialized.

So in your example, the static block of the "topmost" class (C) runs first, then that of B, then the most-derived one.

See that documentation for a detailed description of all the steps that go into loading a class.

(Classes get loaded when they are first actively used.)




回答2:


I think the following example will solve all of your problems:

Before a class is initialized, its superclasses are initialized, if they have not previously been initialized.

Thus, the test program:

class Super {
        static { System.out.print("Super "); }
}
class One {
        static { System.out.print("One "); }
}
class Two extends Super {
        static { System.out.print("Two "); }
}
class Test {
        public static void main(String[] args) {
                One o = null;
                Two t = new Two();
                System.out.println((Object)o == (Object)t);
        }
}

prints:

Super Two false

The class One is never initialized, because it not used actively and therefore is never linked to. The class Two is initialized only after its superclass Super has been initialized.

For more details visit this link

Edit details: Removed confusing lines.




回答3:


From the Java Language Specification:

Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables) declared in the class. Initialization of an interface consists of executing the initializers for fields (constants) declared there.

Before a class is initialized, its superclass must be initialized, but interfaces implemented by the class are not initialized. Similarly, the superinterfaces of an interface are not initialized before the interface is initialized.

The process is described in more detail in the Java Virtual Machine Specification.



来源:https://stackoverflow.com/questions/8550567/what-does-when-a-class-is-loaded-actually-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!