Java class loading process.
Reference books: deep understanding of Java virtual machines
The error in the book is p222.
Field parsing: when parsing a field that has not been parsed, the book first resolves the class_index of the field table,
However, the field indicates no class_index,
The symbolic reference of the field following the bytecode command is constant_field_info.
First, the class_index is parsed.
This process is complicated. To understand this process, you need to have a better understanding of the class file structure, especially the field table.
1. The overall process of tiring Loading
Load -- verify -- prepare -- parse -- initialize -- use -- uninstall
(Note that it is not necessary to wait for a stage to complete before proceeding to the next stage. Some of them are cross-cutting, such as parsing, which may be resolved during use)
I. Loading
(1) Find the binary stream of this class by the full qualified name of a class
(2) Replace the byte Transfer of this class file with the data structure of the runtime in the Method Area (which is understandable)
(3) generate a class object that represents this class in the memory (in the Java hotspot virtual machine, this object is in the Method Area) as the portal of the method area.
Ii. Verification
(1) Verify that the byte stream complies with the format of the class file
(2) Metadata verification whether this class has a parent class (all classes except object must have a parent class)
Does this class inherit final?
Is the inherited interface or abstract class method implemented?
Fields in the class and whether the method is in conflict with the parent class (for example, overwrite the final method of the parent class, cross-overwrite, or overload that does not comply with the requirements)
......
In a word, I think it is in line with the Java metadata syntax (class inheritance, method overwrite, overload ,)
(3) bytecode verification is to check whether the semantics in the Java method is feasible and logical.
(4) symbol reference verification occurs when the Virtual Machine converts the symbol reference to a direct reference.
Iii. Preparation
At this stage, the class variables are allocated with memory and the initial values of the class variables are set (default value)
Instance variables will be allocated to the heap during Object Instantiation (there will also be a process of setting the zero value)
In special cases, if a class field has a constantvalue attribute, it will directly initialize the value specified for this attribute
For more information about constantvalue, see my analysis of the field table.
Iv. Analysis
JVM parses the symbol reference before executing the bytecode command for symbol reference.
It is the symbolic reference (in the constant pool) for the operation after the bytecode is parsed ).
What is resolution?
It is the process in which the Virtual Machine replaces the symbol reference in the constant pool with the direct reference.
Symbol reference: A group of symbols is used to describe the referenced target and has nothing to do with the memory layout. For example, a symbolic reference of a class is its fully qualified name,
Constant_class_info is displayed in the constant pool. (Refer to my analysis on constant pool)
Direct reference: You can direct the pointer to the target, relative offset, or a handle. Related to the memory layout implemented by virtual machines
Class or interface parsing:
Assume that the class of the current Code is D. If you want to resolve a symbolic reference to a direct reference of the class or interface C:
(1) If C is not an array, pass the fully qualified name of C to the Class Loader area of D for loading.
(2) If it is an array type, a class loader of D should first load the array type. Then, the virtual machine generates an array object representing the dimension and element of this array.
(3) Verify that D has access to C.
Field parsing:
To parse a field, first parse the class_index referenced by this symbol. That is, the symbolic reference of the class or interface to which the field belongs. Mark this class or interface as C.
(1) If C contains a field with a simple name and a field descriptor that matches the target (the Field table is queried at this time), a direct reference of this field is returned.
(2) Otherwise, if the interface is implemented in C, find it in the interface.
(3) Otherwise, search for the parent class.
(Class and Interface) method parsing:
Similar to a field.
V. Initialization
<Clinit> and <clinit> are called by the compiler to automatically collect the values of all class variables in the class and combine the statements in the static statement block.
<Clinit> not required.
<Clinit> unlike <init>, it does not need to call the parent class constructor. It ensures that the parent class has been executed before the <clinit> of the subclass.
In <init>, the <init> of the parent class is called first, then the value assignment action, the initialization is fast (the two are displayed in the order), and the statements in the constructor
You can check the bytecode for comparison.
This article from the "thick and thin hair" blog, please be sure to keep this source http://duanzhenyue.blog.51cto.com/9360091/1550118
Detailed description of the class file loading process