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

How to understand the difference between init and clinit in Java JVM virtual machine

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to understand the difference between init and clinit in the Java JVM virtual machine, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Called when jvm first loads the class file, including static variable initialization statements and static block execution

Called when the instance is created, including calling the new operator; calling the newInstance () method of the Class or Java.lang.reflect.Constructor object; calling the clone () method of any existing object; and deserializing through the getObject () method of the java.io.ObjectInputStream class.

Is the object constructor method, that is, the init method is executed when the program executes new when an object calls the constructor method of the object class, but the class constructor method, that is, class loading-verification-parsing-initialization is performed in jvm, and the clinit method is called by jvm in the initialization phase.

Is the instance instance constructor, which parses and initializes non-static variables, while clinit is the class class constructor that initializes static variables, static blocks of code. Is a static method added by javac and called by JVM after the class is loaded. You can see this method using the bytecode outline tool in class bytecode. Note that it is added only when a class requires static initialization, as shown in the following code:

Public class Test1 {

Static int x = 1

Public static void main (String [] args) throws Exception {

}

}

Public class Test2 {

Static final int x = 1

Public static void main (String [] args) throws Exception {

}

}

There is in the Test1 class because its variable x needs to be initialized with 1; Test2 has no method because it x is a constant. Another thing is that Class.forName has a boolen intialize parameter that determines whether the class should be initialized after loading.

In the preparation phase, the variables have been assigned the initial values required by the system, while in the initialization phase, class variables and other resources are initialized according to the programmer's subjective plan through the program. or it can be expressed from another point of view: the initialization phase is the process of executing the class constructor method.

The method is generated by the combination of the assignment action of all class variables in the class automatically collected by the compiler and the statements in the static statement block (static {} block). The order in which the compiler collects is determined by the order in which the statements appear in the source file. The static statement block can only access the variables defined before the static statement block, and the variables defined after it can be assigned in front of the static statement block, but cannot access the following code

Public class Test {

Static {

ISomeo / assign values to variables can be compiled normally.

System.out.print (I); / / the compiler will prompt "illegal forward reference"

}

Static int iTunes 1

}

The virtual machine JVM ensures that the method of the parent class is executed before the method of the child class is executed. So the class of the first method to be executed in the virtual machine must be java.lang.Object. Since the method of the parent class is executed first, which means that the static statement block defined in the parent class takes precedence over the variable assignment operation of the child class, in the following code, the value of field B will be 2 instead of 1.

Static class Parent {

Public static int Aspir1

Static {

Aspir2

}

Static class Sub extends Parent {

Public static int B = A

}

Public static void main (String [] args) {

System.out.println (Sub.B)

}

}

Note: the properties in the interface are constants of type static final and have been initialized in the preparation phase.

Static statement blocks cannot be used in the interface, but there are still assignment operations initialized by variables, so the interface generates methods as well as classes. But an interface differs from a class in that the method that executes the interface does not need to execute the method of the parent interface first. The parent interface is initialized only when the variables defined in the parent interface are used. In addition, the implementation class of the interface also does not execute the methods of the interface at initialization.

Principle of JVM class loading

1) the life cycle of a class consists of seven stages: Loading, Verification, Preparation, Resolution, Initialization, Using and Unloading.

2) when a Java program needs to use a class, JVM ensures that the class is loaded, connected (validated, prepared, and parsed), and initialized.

3) loading phase: get the binary byte stream of a class through the fully qualified name of the class; transform the static storage structure represented by the byte stream into the runtime data structure of the method area; generate a Class object representing the class in the java heap as the access to the data in the method area

4) Verification phase: verification is to ensure that the information contained in the bytestream of the Class file meets the requirements of the current virtual machine, and will not endanger the security of the virtual machine itself; including file format verification, metadata verification, bytecode verification, symbol reference verification; if the bytestream does not conform to the storage format of the Class file, throw a java.lang.VerifyError exception or its subclass exception.

5) preparation phase: the preparation phase is the stage that formally allocates memory for class variables and sets the initial values of class variables (zero values for each data type), which will be allocated in the method area.

6) parsing phase: the parsing phase is the process of replacing symbolic references in the constant pool with direct references in the virtual machine. Symbolic reference: a symbolic reference uses a set of symbols to describe the referenced target, which can be any form of literal quantity, as long as it can be used to locate the target without ambiguity. Symbolic references are independent of the memory layout implemented by the virtual machine, and the target of the reference is not necessarily loaded into memory. Direct reference: a direct reference can be a pointer directly to the target, a relative offset, or a handle that can be located indirectly to the target. If there is a direct reference, the target of the reference must already exist in memory.

7) initialization phase: the initialization phase is the process of executing the class constructor () method.

Initialization is the key point, and the following points need to be clear:

1) () the method is generated by the combination of the assignment actions of all class variables in the class automatically collected by the compiler and the statements in the static statement block (static {} block). The order in which the compiler collects is determined by the order in which the statements appear in the source file. A static statement block can only access variables defined in front of a static statement block, and variables defined after it can be assigned in the previous static statement block, but cannot be accessed.

2) unlike the instance constructor (), the method does not need to be displayed to call the parent class constructor, and the virtual machine ensures that the parent class () has been executed before the () method of the subclass is executed.

3) () method is not necessary for a class or interface, and if there is no static statement block or variable assignment in a class, the compiler may not generate a () method for the class.

4) the () execution of the interface does not need to execute the () method of the parent interface first, and the parent interface will be initialized only when the variables defined in the parent interface are used. The implementation class of the interface also does not execute the interface's () method when initialized.

5) the virtual machine ensures that the () method of a class is correctly locked and synchronized in a multithreaded environment. If multiple threads initialize a class at the same time, only one thread will execute the class's () method. Other threads need to block waiting.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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