Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of Class loading Mechanism of java Virtual Machine

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the introduction of the class loading mechanism of the java virtual machine". In the daily operation, I believe that many people have doubts about the introduction of the class loading mechanism of the java virtual machine. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "the introduction of the class loading mechanism of the java virtual machine". Next, please follow the editor to study!

The timing of class loading

When it comes to class loading, we have to mention the life cycle of class loading. Load-verify-prepare-parse-initialize-use-unload. Seven stages. The book says that the positions of parsing and initialization are interchangeable in order to support the dynamic binding of the java language. I don't know this for the time being. I won't do any research first.

However, let's explore under what circumstances the initialization will be triggered immediately by class loading.

The java virtual machine specification clearly specifies that classes must be initialized immediately in five cases:

When you encounter four bytecode instructions such as new, getstatic, putstatic or invokestatic, if the class is not initialized, it will trigger the class to initialize first. These four instructions correspond to the most common java code scenarios: when instantiating an object with the new keyword, reading or setting a static variable of a class (the variable decorated by static final does not count, because it is placed in the constant pool at compile time), and calling the static method of a class.

When a class is radially called using the method of the java.lang.reflect package, if it is not initialized, it will be initialized first

When initializing a class, if it is found that its parent class has not been initialized, it will initialize its parent class first.

When the virtual machine starts, the user specifies a main class to execute (the class of the main () method), which initializes the main class first

If the result of the last parsing of the java.lang.invoke.MethodHandle instance is the method handle of REF_getstatic, REF_putstatic, and ERF_invokestatic, and the class corresponding to the handle of this method is not initialized, it will be initialized first.

The scenario in the above 5 becomes an active reference to a class, but otherwise, the way the class is referenced does not trigger initialization and becomes a passive reference.

Let's look at the code:

Package chapter.Seven

Class SuperClass {static {System.out.println ("SuperClass init");} public static int value=123; public static final String NAME= "QuellanAn";}

Class SubClass extends SuperClass {static {System.out.println ("SubClass init");}}

Public class NotInitialzation {public static void main (String [] args) {System.out.println (SubClass.value);}}

Call "SubClass.value" in the main method, where value is a static variable of SuperClass. So the first point above is triggered, so the SuperClass class is initialized. Although it is called through SubClass, the SubClass class does not trigger any of the above 5 points, so it is not initialized.

Results:

SuperClass init123

After looking at one, change the main () method, and everything else remains the same.

Public class NotInitialzation {public static void main (String [] args) {System.out.println (SubClass.NAME);}}

Also based on the first point above, you can see that NAME is a static constant that exists in the constant pool at compile time (when the class file is formed). So SuperClass will not be initialized.

Results:

QuellanAn

The above example is a good way to see when this class is initialized and when it is not. Types are initialized only in the above 5 cases.

In addition, when a class is initializing, it requires that all its parent classes have been initialized, but when the interface is initialized, it does not require all the parent interface to initialize, it just needs to initialize which parent interface is actually used.

The process of class loading

The class loading process is divided into seven stages, but the important previous loading-verification-preparation-parsing-initialization of the first five. Let's explain it in turn.

Load

During the load phase, the virtual machine needs to do the following three things:

1. Gets the binary byte stream that defines this class through the fully qualified name of a class. (fully qualified name: I was a little confused when I heard the word for the first time. I looked it up on the Internet and found that it meant something absolute, such as

The name of the Java class package: com.linux.struct.sort.bubblesort, which is invoked from the most original and top layer to the specific object, which is the fully qualified name)

two。 Converts the static storage structure represented in the binary byte stream into the method area runtime structure.

3. A java.lang.Class object representing this class is generated in memory as an access entry for each data of the class in the method area.

Verification

Verification is the first step in the connection phase, and the main purpose of verification is to ensure that the information contained in the binary file stream converted from class files meets the requirements of the current virtual machine. And will not endanger the security of the virtual machine.

The verification phase mainly completes four stages of verification: file format verification, metadata verification, bytecode verification and symbol reference verification.

Prepare for

The preparation phase is the stage that formally allocates memory space for class variables and sets the initial values of class variables. The memory used by these variables will be allocated in the method area. It should be noted that the memory allocation at this stage includes class variables rather than instance variables. Instance variables are assigned to the java heap along with the object when the object is instantiated. And the initial value here is "usually" the zero value of the data type, such as:

Public static int value=123

The initial value after the value preparation phase is 0, instead of 123. Value is assigned to 123 when initialized. Because the putstatic instruction that assigns value to 123 is not executed until it is initialized.

Analysis

The parsing phase is the process that the virtual machine replaces the symbolic references in the constant pool with direct references.

Initialization

In the initialization phase, the class constructor () method is executed. Class variables are assigned and static statement blocks (static {} fast) are executed.

At this point, the study on the "introduction to the class loading mechanism of the java virtual machine" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report