I. Life cycle of a class
The class starts from being loaded into the virtual machine memory and has the following () life cycle until the memory is unloaded:
The Java Virtual Machine specification does not impose constraints on when the first phase of the class load is required, but instead gives the virtual machine a grasp of the detail implementation.
However, for the initialization phase, the virtual machine has the following 5 conditions that must be "initialized" to the class immediately:
(1) When encountering the 4 bytecode instructions of new, getstatic, putstatic or invokestatic, it is necessary to immediately trigger the initialization of the class if it is not initialized.
Common scenario: Use new to instantiate an object, read or set a static field for a class (in addition to the final field that the compiler has processed), and to invoke a static method of a class.
(2) The initialization of a class is also triggered when a class is called with Reflection (Java.lang.reflect package).
(3) When a class is initially initialized, the initialization of the parent class is triggered first when it is discovered that the parent class is not initialized.
(4) When the virtual machine starts, the user needs to specify a main class to run, including the Main method, and the virtual opportunity initializes the class.
(5) When using JDK1.7 's dynamic language support, assume that the final parsing result of a Java.lang.invoke.MethodHandle instance ref_getstatic, Ref_putstatic, ref_ The Invokestatic method handle that triggers the initialization of the corresponding class.
The behavior of the above 5 scenarios is called an active reference to a class, except that the reference will not trigger the initialization of the class, known as a passive reference.
Below is one of the passive references:
class superclass{static {System.out.println ( "superclass init!" ); } public static int value = 123 ;} Class Subclass extends superclass{static {System.out.println ( "subclass init!" );}} public class notinitialization { public static void main (string[] args) {System.out.println (Subclass.valu e); }}
Output Result:
Superclass init!
123
Two. Loading process of Class 1. Loading
The onboarding phase carries out the process:
(1) obtain a binary byte stream that defines this class by the fully qualified name of a class;
(2) Transform the static storage structure represented by this byte stream into the runtime data structure of the method area;
(3) Generate a Java.lang.Class object representing this class in memory as an access point for various data of this class of method area.
2. Verification
Validation is done to ensure that the byte stream includes information that meets the requirements of the current virtual machine and is secure.
There are about four stages of verification
(1) file Format verification: Verify that the byte stream conforms to the specification of the class file format.
(2) meta-data validation: The semantic analysis of the information of the description of bytecode, the purpose of which is to check the metadata information (data type) of the class;
(3) Bytecode verification: Validation analysis of the class method body;
(4) Symbolic reference validation: matching checks on information outside the class itself (various symbol references in a constant pool).
An optimization can be made here: because the validation phase is time-consuming or quite large, if it has been repeatedly used and validated, it is possible to skip this step and speed up class loading.
3. Preparation
Formally allocates memory for class variables (static) and sets the initial values of class variables, where the class variables are final decorated.
4. Parsing
The process of replacing a symbolic reference with a direct reference.
5. Initialization
<clinit>()the process of running a class construction method. That is, the run of the static block statement and the copy operation of the variable.
[Reading notes] Java class loading process