I. Life cycle of a class
Class starts from being loaded into the virtual machine memory, and has the following () life cycles until the memory is unloaded:
This is called the load process for the class, resolution-- --------
The Java Virtual Machine specification does not impose constraints on when it is necessary to start the first phase of class loading, but instead gives the virtual machine a grasp of the specific implementation.
However, for the initialization phase, the virtual machine has the following 5 cases where the class must be "initialized" immediately:
(1) When encountering the 4 bytecode instructions of new, getstatic, putstatic or invokestatic, it is necessary to trigger its initialization immediately if the class 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 master class that executes the main method and initializes the class with a virtual opportunity.
(5) When using JDK1.7 's dynamic language support, if a java.lang.invoke.MethodHandle instance finally resolves the results 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.
The following is an alternative to a passive reference:
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.value);
}
}
Output Result:
Superclass init!
123
Two. Loading process of Class 1. Loading
During the load phase:
(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 the access entry for various data of this class in the method area.
2. Verification
Validation is to ensure that the byte stream contains information that conforms to 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 bytecode description, the purpose 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 very large, if it has been reused multiple times and is validated code can skip this step and speed up the 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
The process of executing the () class construction method, which is the execution of the static block statement and the copy operation of the variable.
From for notes (Wiz)
[Reading notes] Java class loading process