Home > Java > Java Tutorial > body text

What is the java class loading mechanism?

(*-*)浩
Release: 2020-09-17 11:59:26
Original
18037 people have browsed it

This article will introduce the steps of Java's class loading mechanism. Please see the following details.

What is the java class loading mechanism?

Class loading mechanism

From the time a class is loaded into the virtual machine memory to the time it is unloaded from the memory, its entire The life cycle includes seven stages: Loading, Verification, Preparation, Resolution, Initialization, using, and Unloading. The three parts of verification, preparation and parsing are collectively called Linking. The sequence of these seven stages is shown in the figure below:

What is the java class loading mechanism?

The timing of class loading

Active reference: After a class is actively referenced, the initialization process will be triggered (loading, verification, and preparation need to start before)

1) When encountering new, getstatic, putstatic or When using the four bytecode instructions of invokestatic, if the class has not been initialized, its initialization needs to be triggered first. The most common Java code scenarios that generate these four instructions are: when using the new keyword to instantiate an object, reading or setting static fields of a class (except for static fields that are modified by final and have been put into the constant pool by the compiler) ), and when calling a static method of a class.

2) When using the method of the java.lang.reflect package to make a reflective call to a class, if the class has not been initialized, its initialization needs to be triggered first.

3) When initializing a class, if you find that its parent class has not been initialized, you need to trigger the initialization of the parent class.

4) When the virtual machine starts, the user needs to specify a main class for execution (the class containing the main() method), and the virtual machine will initialize this class first.

5) When using the dynamic language support of jdk7, if the final parsing result of the java.lang.invoke.MethodHandle instance is the method handle of REF_getStatic, REF_putStatic, REF_invokeStatic, and the class corresponding to this method handle has not been Initialization requires trigger initialization first.

Passive reference: If a class is a passive reference, the class will not trigger the initialization process

1) Referring to the static fields of the parent class through the subclass will not cause the subclass to be initialized. For static fields, only the class that directly defines the field will be initialized, so when we refer to the static field defined in the parent class through a subclass, only the initialization of the parent class will be triggered, not the initialization of the subclass.

2) Referring to a class through an array definition will not trigger the initialization of this class.

3) Constants will be stored in the constant pool of the calling class during the compilation phase. In essence, they do not directly reference the class that defines the constant, so the initialization of the class that defines the constant will not be triggered.

The above is the detailed content of What is the java class loading mechanism?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!