Class initialization vs. class object initialization

Source: Internet
Author: User

Life cycle of a class:

Loading--> validation, preparation, parsing--> initialization--> using--> Uninstall

Class initialization in 5 cases:

1. When you encounter the 4 bytecode directives for new, getstatic, putstatic, or invokestatic, if the class has not been initialized, you need to first trigger its initialization. The most common Java code scenario for generating these 4 directives is when instantiating an object with the new keyword, reading or setting a static field of a class (except for final adornments), and calling a static method of a class

2. When a reflection call is made to a class using the Java.lang.reflect package method, if the class is not initialized, you need to trigger its initialization first

3. When initializing a class, it is necessary to trigger the initialization of its parent class if it finds that its parent class has not yet been initialized

4. When the virtual machine is started, the virtual machine Initializes a class of the Main method

5. When using jdk1.7 Dynamic language support, if a Java.lang.invoke.MethodHandler instance resolves the result ref_getstatic, Ref_putstaic, and ref_involestatic the method handle , and the class that corresponds to the method handle is not initialized, you need to first trigger its initialization


The class initialization phase is the process of executing the Class Builder <clinit> () method.

The <clinit> () method is generated by the compiler automatically collecting statement merges in the assignment actions of all class variables in a class and in static statement blocks (static{} blocks), and 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 a variable that is defined before a static statement, and a variable that is defined after it, which can be assigned a value in the preceding static statement block, but cannot be accessed.

The <clinit> method differs from the constructor of the class (or the instance constructor <init> () method), and it does not need to be shown to invoke the parent class constructor, which guarantees that the < of the parent class precedes the execution of the <clinit> () method of the subclass. The Clinit> () method has been completed.

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 of the subclass.

The <clinit> () method is not required for a class or interface, and if there is no static statement block in a class and there are no class variables, then the compiler can not produce the <clinit> () method for this class

A static statement block is not 鞥 in an interface, but there is still an assignment action initialized by a variable, so the interface will generate the <clinit> () method just like the class, but the <clinit> () method that executes the interface does not need to execute the parent interface's < Clinit> () method. The parent interface is initialized only if the variables defined in the parent interface are used. In addition, the interface's implementation class does not perform the interface's <clinit> () method as it initializes.

Virtual opportunity guarantees that a class's <clinit> () method is properly locked and synchronized in a multithreaded environment, and if multiple threads are initializing a class at the same time, there will only be one thread to execute the <clinit> method of the class, and other threads will need to block the wait. Know that the active thread executes the Clinit () method complete. If a class's <clinit> () method has a lengthy operation, it can cause more than one process to block.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.