Overview
In the Java language, the loading, connection, and initialization of types are done during program runs. The virtual machine loads the data of the description class from the class file or other place into memory, verifies the data, parses and initializes it, and finally forms the Java type that can be used directly by the virtual machine, which is the class loading process of the virtual machine.
time of class loading
The order of the 5 phases of loading, validating, preparing, initializing, and unloading is determined, and the class's loading process must begin in this order, and the parsing phase is not necessarily: it can start after the initialization phase in some cases. These phases are usually mixed in a cross-section, usually invoking and activating another phase during one phase of execution.
The Java Virtual Machine specification does not enforce constraints to start the class loading process. However, for the initialization phase, the virtual machine specification is strictly defined and there are only 5 cases where the class must be initialized immediately:
- When instantiating an object using the New keyword, when reading or setting a static field of a class, and when invoking a static method of a class.
- When you use the Java.lang.reflect package method to make a reflection call to a class.
- When initializing a class, it is necessary to trigger the initialization of its parent class if it finds that its parent class has not yet been initialized.
- When the virtual machine is started, the user needs to specify a main class to execute (the main class that contains the main () method), and the virtual opportunity initializes the main class first.
- When using dynamic language support for JDK1.7, if a java.lang.invoke.MethodHandle instance finally resolves to a method handle of ref_getstatic/ref_invokestatic/ref_putstatic , and the class that corresponds to the method handle is not initialized, you need to trigger its initialization first.
In addition to these 5 scenarios, all methods of referencing a class do not trigger initialization.
the process of class loading
I drew a mind map to describe the process, such as:
If the browser does not see clearly, save to the computer on the open picture.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Detailed Java Virtual machine class loading