In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the second half of JDK15 class: what is the process of preparation, parsing, initialization and uninstallation". In daily operation, I believe many people have doubts about what is the process of preparation, parsing, initialization and uninstallation of JDK15 class. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubts of "the later life of the JDK15 class: what is the process of preparing, parsing, initializing, and uninstalling"! Next, please follow the editor to study!
Prepare for
Two goals:
Allocate memory for static variables of classes that are already in the method area
Set the initial value for the static variable. The initial default values for different data types are as follows:
Case
Public static final int value = 123
The value of value is 0 after preparation, not 123, and 123 after initialization.
However, if modified by final, if there is an initial value, the initial value is stored in the constantValue property at compile time, and the value of constantValue is assigned to the field in the preparation phase (here assign value to 123).
Analysis
Converts symbolic references in the constant pool to direct references.
Symbol reference
A set of unambiguous symbols to describe the referenced target, independent of the implementation of JVM
Direct reference
A pointer directly to the target, a relative offset, or a handle that can be located indirectly to the target, related to the JVM implementation
Mainly for: class, interface, field, class method, interface method, method type, method handle, call point qualifier.
Initialization
Actually start executing the Java program code (or bytecode) defined in the class.
The initialization of a class is to assign an initial value to the static variable of the class, and the initialization phase is the process of executing the class constructor.
If the class has not been loaded and linked, execute it first
If there is a parent class in the class and the parent class is not initialized, initialize the parent class first
If there are initialization statements in the class, execute them in turn
If the interface
When a class is initialized, the interface it implements is not initialized first
When an interface is initialized, its parent interface is not initialized. Only when the program uses the variables in the interface or calls the interface method for the first time will it cause the interface initialization.
Calling the loadClass method of the Classloader class to load a class does not initialize the class and does not belong to the active use of the class
The clinit () method is automatically generated by the compiler to collect the class variable assignment statements in the static code block in the class and the static variable assignment statements in the class:
In the preparation phase, the static variable in the class has completed the default initialization
In the initialization phase, the clinit () method explicitly initializes the static variable.
The initialization time of the class
The ways that Java programs use classes are as follows:
Active use
Passive use
JVM must initialize each class or interface when it is "actively used" for the first time, and passive use of a class does not result in class initialization.
Active use scenario
Create a class instance
If the static variable that accesses a class or interface is a final constant, and the constant is in the constant pool during compilation, there is no reference to the class that defines the constant, so the initialization that defines the constant class will not be triggered.
Call the static method of the class
Reflect a class
Initialize a subclass of a class, but the parent class has not been initialized yet
The main class that runs when JVM starts (equal to Article 3)
Defines the interface of the default method, when the interface implements the class initialization
FAQ
The clinit () method is generated by the combination of the assignment action of all class variables in the IDE collection class and the statements in the static statement block. The order of IDE collection is determined by the order in which the statements appear in the source file.
The static code block can only access variables that appear before the static code block, and the variables defined after it can be assigned in the preceding static statement block, but cannot be accessed
Public class Test {static {I = 0; System.out.println (I); / / compilation failed: illegal forward reference} static int i = 1;}
The instance constructor init () needs to explicitly call the parent class constructor, while the clinit () of the class does not need to call the parent class constructor. JVM ensures that the clinit () of the parent class is executed before the clinit () method of the subclass is executed.
So the class of the first clinit () method to be executed in JVM must be java.lang.Object.
If a class / interface has no static code block and no assignment of static member variables, the compiler will not generate the clinit () method for this class.
The interface also needs to be explicitly initialized for the static member variables defined in the interface through the clinit () method.
Static code blocks cannot be used in the interface, but there are still assignment operations initialized by variables, so the interface generates the clinit () method just like the class. The difference is that the clinit () method of the interface does not need to execute the clinit () method of the parent interface first, and the clinit () method of the parent interface is executed only when the static member variable in the parent interface is used.
JVM ensures that the clinit () method of a class is properly locked and synchronized in a multithreaded environment. When multiple threads initialize a class at the same time, only one thread executes the class's clinit () method, and the other threads are blocked to wait until the active thread finishes executing the clinit () method.
Although other threads will be blocked, as long as a clinit () method is executed, other threads will not enter the clinit () method after waking up. Under the same class loader, a type is initialized only once.
Unloading of class
When the Class object representing a class is no longer referenced, the life cycle of the Class object ends and the corresponding data in the method area is unloaded. Classes loaded by Jvm's own class loader will not be unloaded, and classes loaded by a user-defined class loader can be unloaded.
At this point, the study on "the second half of the life of the JDK15 class: what is the preparation, parsing, initialization, and uninstallation process" 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.