JVM Explanation II: Timing of class initialization

Source: Internet
Author: User

The previous section spoke about the steps of the Java class initialization, so when does the initialization work?

When you use a class or interface in the following 6 ways, the system initializes the class or interface:

Creates an instance of the class. The methods for creating an instance of a class include using new to create an instance, creating an instance from reflection, or deserializing it by creating

Call a class method of a class (static method)

To access a class or an interface's classes variable, or assign a value to the class variable

Use reflection to force the creation of a Java.lang.Class object corresponding to a class or interface. For example: Class.forName (Xxx.xxx.Person); If the person class has not yet been initialized, this line of code will initialize the person class with the knife, returning the Java.lang.Class object corresponding to the person class.

Initializes a subclass of a class. When a class is initialized, the system checks to see if the class has a parent class and whether the parent class is initialized, and the end result is that all parent classes of the subclass's ancestors will be initialized.

Use the Java.exe command directly to run a main class. When this main class is run, the main class is initialized first. For example, run the main function in a class.

In addition, the following situations need to be highlighted.
For a final modified class variable, if the value of the class variable can be determined at compile time, then the class variable is equivalent to "macro variable" and can also be understood as "constant". Java goes together to replace the occurrence of this class variable with its value directly at compile time, so even if the program uses that static variable, it will not cause the class to initialize.

classMyTest {Static{System.out.println ("Use final static variables----static initialization blocks ..."); }    //use a string to assign a literal to the static final class variable    Static FinalString compileconstant = "Final static variable";}classMyTest2 {Static{System.out.println ("No final static variable is used----static initialization block ..."); }    //use a string to assign a literal to the static final class variable    StaticString compileconstant = "Static variable";} Public classCompileconstanttest { Public Static voidMain (string[] args) {//Accessing and outputting compileconstant class variables in MyTestSystem.out.println (mytest.compileconstant);//①System.out.println (mytest2.compileconstant);//②    }}/*Result: The final static variable does not use the final static variable----static initialization block ... Static variables*/
Code

The above program has a variable of type string, but the variable uses the final decoration, and static is used to modify it, so you can determine at compile time that the variable is treated as a "macro variable".

When a static variable (class variable) uses the final decoration, but its value is not determined, accessing the variable through that class causes the class to initialize, for example

Static String CompileConstant2 = System.currenttimemillis () + "";

This causes the class to be initialized.

JVM Explanation II: Timing of class initialization

Related Article

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.