The life cycle of a class is shown in the following breasts, it should be noted that this is only the beginning of the sequence of stages, not a stage to wait until the last stage of execution before you can begin. So there may be a situation where one stage is running and the other phase is starting to run.
The Java Virtual Machine specification does not specify when classes are to be loaded, but it is strictly defined that there are only 5 cases where the class must be initialized immediately (while loading, validating, preparing, parsing is done before this):
1. When instantiating an object with the New keyword, read or set a static field for a class, or call a method. It is particularly important to note that the final modified constants are stored in the constant pool during the compilation phase, so a static final field referencing a class does not load a class. See also: http://blog.csdn.net/u012852385/article/details/49049351
2. When invoking a class using the reflection method, initialize it first if the class has not been initialized.
3. If the parent class is not initialized when calling a class, initialize its parent class first.
4, when the virtual machine starts, the user needs to develop a main class to execute (including the main method of the class), the virtual opportunity to initialize the main class first
5, when using jdk1.7 's dynamic language support, if a java.lang.invoke.MethodHandle instance of the final parsing result is ref_getstatic, ref_putstatic, ref_ Invokestatic the method handle, and the class for the method handle has not been initialized, you need to start its initialization.
The following is a concrete class loading process:
Load
This phase of the virtual machine needs to complete the following 3 things:
1. Obtain a binary byte stream that defines this class by using 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
Verify
The purpose of the verification is to ensure that the information contained in the byte stream of the class file conforms to the requirements of the current virtual machine and does not compromise the security of the virtual machine itself. The main consists of 5 stages:
1. File Format Verification
Verifies that the byte stream conforms to the format of the class file and can be processed for the current version of the virtual machine. For example ":
A. Do you want to start with magic 0xCAFEBABE
B. is the primary and secondary version number within the current Virtual machine processing range
Whether there are constant types that are not supported in constant pool constants in the C.class file, etc.
2. Meta-data validation
Semantic analysis of the information described by bytecode at the class level, such as:
A. Does this class have a parent class?
B. Does this class inherit a class that does not allow inheritance, such as the final class
C. If the class is not an abstract class implements all the methods that the parent class or interface needs to implement
D. Whether the class's fields or methods contradict the parent class
3. Byte code Verification
At the method level of the class, the method body is verified by the analysis of data flow and control flow to ensure that the program semantics are legal and logical. For example: to ensure that the data type of arbitrary time operand and instruction code sequence can work together, for example, will not appear in the stack is an int, but a long type of case, etc.
4. Symbol Reference Verification
This phase of validation occurs when a symbolic reference is converted to a direct reference, and the conversion occurs during the parsing phase, which is used to ensure that it can be parsed. Usually check the content:
A, the fully qualified name in the symbol reference through the string description can find the corresponding class
b, whether there is a field descriptor in the specified class that conforms to the method, and the method and field described by the simple name
C, whether the class, field, or method in the symbol reference can be accessed by the current class. The scope of permissions is as follows:
Get ready
The preparation phase formally allocates memory for the class variable (static variable) and sets the class variable initial value, and the memory used by these variables is allocated in the method area.
Data type |
0 value |
Data type |
0 value |
Int |
0 |
Boolean |
False |
Long |
0L |
Float |
0.0f |
Short |
(short) 0 |
Double |
0.0d |
Char |
' \u0000 ' |
Reference |
Null |
Byte |
(byte) 0 |
|
|
Analytical
The parsing phase is the process by which the JVM replaces symbolic references within a constant pool with direct references. Let's look at the difference between a symbolic reference and a direct reference:
Symbol Reference: A set of symbols to describe the referenced target, the symbol can be any form of literal, as long as the target can be located, the target is not necessarily loaded into memory. The literal form of a symbol reference is explicitly defined in the Java Virtual Machine specification's class file format.
Direct reference: Can be a pointer directly to the target, a relative offset, or a handle that can be simply positioned to the target. At this point the target must already exist in memory.
Parsing actions are primarily for classes or interfaces, fields, class methods, interface methods, method types, method handles, and call Point qualifier 7 class symbol references.
Initialization
The initialization phase is the last step in the class loading process, in which the rest of the action is completely dominated and controlled by the virtual machine, in addition to the user application being able to participate through the custom class loader during the load phase. In the initialization phase, the Java code (or bytecode) that is defined in the class is actually started.
At this stage, the static statement is executed first, including the definition of the class variable and the static{} statement block, and then the constructor of the class is executed. A static statement block can only access variables that are defined before a static statement block, a variable that is defined after it, and a block of static statements in front of it may be assigned, but cannot be accessed. Like what:
public class test{
static {
i = 0;
//system.out.println (i);//This compiler will prompt "illegal forward reference"
}
static int i = 1;
public static void Main (string[] args) {
System.out.println (TEST.I);//output is 1
}
}
Because the parent class always performs initialization first, this means that the static statement of the parent class static statement > Subclass > The constructor method of the parent class > The constructor of the subclass (> indicates that execution time precedes).
public class Testinit extends parents{
static{
System.out.println ("A1");
}
Public Testinit () {
System.out.println ("A2");
}
public static void Main (string[] args) {
New Testinit ();
New Testinit ();
}
}
Class Parents {
static {
System.out.println ("B1");
}
Public parents () {
System.out.println ("B2");
}
}
The execution result of the above code is B1 A1 B2 A2 B2 A2, under the same ClassLoader, a type is initialized only once.
It is also necessary to be reminded that static statement blocks cannot be used in an interface, and unlike classes, the parent interface is initialized only when a variable defined in the interface is used.
The following class loaders are described below
The ClassLoader uses a parental delegation mechanism (parents delegation Model): If a class loader receives a request for a class load, it first does not attempt to load the class itself, but instead delegates the request to the Father Loader, which is the case for each level of the ClassLoader, Therefore, all load requests should eventually be routed to the top-level startup ClassLoader, and only the subclass will attempt to load when the parent ClassLoader feedback that it cannot complete the load request. As shown, the first three are system loaders, the lowest layer can also have the user's own implementation of the loader. Here are the search scopes for the three loaders (you can also modify the parameters to point to different ranges):
A. Start the ClassLoader: Load classes placed in <java_home>\lib
B. Extension class loader: Loading <java_home>\lib\ext directory
c, load load the class library specified on the User class path (ClassPath)
JVM class loading mechanism