The following is from Zhou Zhiming's in-depth understanding of Java Virtual Machines: The class initialization phase is the last step in the class loading process, and the rest of the action is completely dominated and controlled by the virtual machine except during the load phase, in addition to the user application being able to participate through the custom class loader. In the initialization phase, the Java program code (or bytecode) that is defined in the class is really starting to execute.
In the preparation phase, the variable has already assigned the initial value of the system requirement, and during the initialization phase, the class variables and other resources are initialized according to the programmer's subjective plan, or can be expressed from another angle: The initialization phase is the process of executing the class constructor <clinit> () method. We will explain how the <clinit> () method is generated, and here we take a look at some of the features and details of the <clinit> () method that may affect the behavior of the program during the execution, which is relatively closer to the normal developer of the program.
? 1. The <clinit> () method is generated by the combination of the assignment action of all class variables in the compiler's auto-collection class 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 that are defined before a static statement block, a variable that is defined after it, and a block of static statements in front of it may be assigned, but cannot be accessed.
2. The?<clinit> () method differs from the constructor of the class (or the instance constructor <init> () method), which does not need to call the parent class constructor explicitly, and the virtual opportunity guarantees that the parent class's The <clinit> () method has been executed. So the class of the first executed <clinit> () method in the virtual machine is definitely java.lang.Object.
? 3. Because the <clinit> () method of the parent class executes first, it means that the static statement block defined in the parent class takes precedence over the variable assignment operation of the child class.
Public classSuperclass { Public Static intValue = 123; Static{System.out.println (The parent class initializes. "); } PublicSuperclass () {System.out.println (The parent class constructor. "); }} Public classSubclassextendsSuperclass {Static{System.out.println ("Subclass initialization. "); } PublicSubclass () {System.out.println ("Subclass constructor. "); }} Public Static voidMain (string[] args) {Subclass Subclass=Newsubclass ();}//The result is that the parent class takes precedence over the subclass initialization, and the constructor of the parent class takes precedence over the constructor of the subclass, but the initialization takes precedence over the constructor function. The parent class is initialized. Initializes the subclass of the class. The parent class constructor. The subclass constructor.
4. The?<clinit> () method is not required for a class or interface, and the compiler can not generate a <clinit> () method for this class if there is no static statement block in a class and no assignment to the variable.
? 5. Static statement blocks cannot be used in an interface, but there are still assignment operations for variable initialization, so the interface will generate the <clinit> () method as a class. However, unlike the class interface, the <clinit> () method of the execution interface does not require the parent interface's <clinit> () method to be executed first. The parent interface is initialized only if the variables defined in the parent interface are used. In addition, the implementation class of the interface does not execute the <clinit> () method of the interface at initialization time.
? 6.The virtual opportunity guarantees that a class <clinit> () method is properly locked and synchronized in a multithreaded environment, and if multiple threads initialize a class at the same time, only one thread executes the <clinit> () method of the class. All other threads need to block waitUntil the active thread Execution <clinit> () method is complete. If there is a long-time operation in the <clinit> () method of a class, it can cause multiple processes to block, which is often hidden in practical applications. --so you can use static inner classes to implement simple interest mode.
[Reading notes] Java class initialization