Java Virtual machine class load phase-initialization (initialization)

Source: Internet
Author: User

The class initialization phase is the last step in the loading process, where 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 from another angle: the initialization stage is the execution class constructor < The process of the clint> () method.

The <clint> () method is generated by the combination of the assignment of all class variables in the compiler's automatic collection of classes and the statements in the static statement block (static ()), which are collected by the compiler in the order in which the statements appear in the source file, statically

determined by the order.

The <clinit> () method differs from the parent class constructor (or instance constructor <init> method) in that it does not require a display to call the parent class constructor, and the virtual opportunity guarantees that the parent class's < before the subclass's <clinit> method executes. The Clint> () method has been executed. So the first <clinit> () method that is executed in the virtual machine is definitely java.lang.Object.

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 child class.

The <clinit> () method is not necessary for a class or interface, and if there is no static statement block in a class, and there is no assignment to the variable, the compiler can not generate the <clinit> () method for the class.

A static statement block cannot be used in an interface, but there is still an assignment operation that initializes the variable, so the interface generates the <clinit> () method as a class. But interfaces differ from classes in that execution interfaces <clinit> methods do not require

The <clinit> () method of the parent interface is 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.

Sequence opportunities ensure that a class <clinit> () method is correctly locking, synchronized in a multithreaded environment, if multiple threads are initializing a class at the same time, then only one thread executes the <clinit> () method of the class, and the other

Threads are required to block the wait until the thread execution <clinit> () method is complete.

Java Virtual machine class load phase-initialization (initialization)

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.