Java Virtual machine JVM learns initialization of class 04

Source: Internet
Author: User

Java Virtual Machine JVM learns initialization of class 04

initialization of the class

During the initialization phase, the Java virtual machine executes the initialization statement of the class, assigning the initial value to the static variables of the class.

In a program, there are two ways to initialize a static variable:

1. Initialize at the declaration of the static variable;

2. Initialize in a static block of code.

A static variable that has not been explicitly initialized will have the original value.

Declarations of static variables, as well as static blocks of code, are treated as initialization statements for classes, and Java virtual opportunities execute them sequentially in the order in which the initialization statements are in the class file.

initialization steps for a class

1. If the class has not been loaded and connected, load and connect it first.

2. If the class has a direct parent class and the parent class has not been initialized, initialize the immediate parent class first.

3. If there is an initialization statement in the class, execute the initialization statements in turn.

the initialization time of the class

The way Java programs use classes can be divided into two types:

1. Active use

2. Passive use

All Java Virtual machine implementations must initialize each class or interface when it is first actively used by the Java program.

Six cases of active use:

1. Create an instance of the class. 

2. Access a static variable for a class or interface, or assign a value to the static variable. 

3. Invoking a static method of a class

4. Reflection

5. Initialize a subclass of a class

Classes that are marked as startup classes when the 6.Java virtual machine is started

In addition to the above six cases, other ways of using Java classes are treated as passive use of the class, without causing the initialization of the class.

the particularity of the interface

When a Java Virtual machine Initializes a class, all of its parent classes are required to be initialized, but this rule does not apply to the interface.

When a class is initialized, the interface it implements is not initialized first.

When an interface is initialized, its parent interface is not initialized first.

Therefore, a parent interface is not initialized because of its subinterfaces or the initialization of the implementation class, and only when the program first uses a static variable for a particular interface, the initialization of that interface is caused.

static variables of final type

Whether a static variable of the final type is a compile constant or variable affects the execution of the initialization statement block.

If the value of a static variable is a compile-time constant, the type is not initialized (the static block of the class does not execute);

If the value of a static variable is a non-compile constant, that is, only the runtime has a deterministic initialization value, the type is initialized (the static block of the class is executed).

attribution clarity of active use

Active use of a class or interface can be considered only if the static variable or static method accessed by the program is indeed defined in the current class or in the current interface.

ClassLoader class

Calling the LoadClass () method of the ClassLoader class to load a class is not an active use of the class and does not cause the initialization of the class.

Java Virtual machine JVM learns initialization of class 04

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.