当前位置:网站首页>Class loading process of JVM

Class loading process of JVM

2022-06-21 21:00:00 Swarford

1. load

java After the source code is compiled into bytecode , Then the bytecode is loaded into the method area through the class loader , And according to Class File descriptions are created in the heap java.lang.Class object

2. link

2.1. verification :

Verify whether bytecode conforms to virtual machine specification , If it is illegal, an error will be reported ClassFormatError

2.2. Get ready :

2. by static Static variables allocate space , Set the default value
JDK1.6 Static variables in the method area , current JDK1.8 Static variables in the heap
static Allocating space and assigning values to static variables are two steps , The allocation of space is done in the preparation phase , The assignment is done in the construction of the initialization stage class
· If it is static final The basic type is special , Values are determined at compile time , Assignment is done in preparation
· If it is static final Reference type , Then the assignment is also done in the initialization phase

2.3. analysis :

This refers to resolving a symbolic reference in a constant pool to a direct reference ,
Symbol references are just symbols , Don't know class 、 Method in memory , But after parsing , You know the class 、 The exact location of the method in memory

loadClasss() Method load only , But it will not lead to class resolution and initialization
new() Can do parsing and initialization

3. initialization

Initialization is to execute the constructor of a class , The virtual machine guarantees the thread safety of the constructor of this class
When it happened :
Initialization is lazy
main Method time is initialized first
First access to static variables of this class ( No final Of ), Static method
Initialization of subclasses : If the parent class has not been initialized, the parent class will be initialized first
Subclasses access static variables of the parent class , Only the initialization of the parent class will be triggered , Subclasses are not initialized
Class.forName Will cause class initialization (newInstacnce() Method will perform a parameterless construction )
new() Object causes initialization
Will not cause initialization :
visit static final static const ( Basic types 、 character string ) Initialization will not be triggered , They all complete the assignment of variables in the link preparation stage
Use ClassLoader default loadClass Method does not trigger the initialization of the class when it loads the class
Creating an array of classes does not result in initialization
When a subclass refers to a static field of a parent class , Does not trigger subclass initialization , Only the initialization of the parent class will be triggered .
loadClasss() Method load only , But it will not lead to class resolution and initialization

原网站

版权声明
本文为[Swarford]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211922485984.html