The life cycle of the Java class

Source: Internet
Author: User

Life cycle of a class: load, connect (validate, prepare, parse), initialize, use, unload
Active references (with and only) initialization:
1.new, Getstatic, putstatic, invokestatic if the class is not initialized, initialize the new keyword to instantiate the object, read or set the static field of a class (except for the static fields that have been put into the constant pool at compile time), Call a static method of a class
2. If the class has not been initialized by using the Java.lang.reflect package method to emit calls to the class, the initialization
3. When initializing a class, the parent class is not initialized.
4. When the virtual machine starts, the user needs to specify a main class to execute (the class that contains the main method), and the virtual opportunity initializes the main class first

static final int a = 1;//is a compile-time constant
static final int b = "Test". Length ();//is a run-time constant

Passive references:

1. Referencing a static field of a parent class through a subclass does not cause the subclass to be initialized

2. Referencing a class through an array definition does not trigger initialization of this class

3. Constants are stored in the constant pool of the calling class at compile time and are not inherently referenced directly to classes that define constants, so initialization of classes that define constants is not triggered

Parent Class Code:

1  Public Class superclass{2      Public Static int values = 123; 3      Public Static Final String Hello = "Hello"; 4     Static {5         System.out.println ("Superclass init"); 6     }7 }    

Subclass Inherits Superclass:

1  Public extends superclass{2     Static {3         System.out.println ("Subclass Init"); 4     }5 }    

Test class:

1  PublicClass test{2      Public Static voidMain (string[] args) {3System.out.println (subclass.values);//referencing a static field of a parent class through a subclass4Superclass SCA =NewSUPERLASS[10];//referencing a class through an array definition does not trigger initialization of this class5System.out.println (Superclass.hello);//using Constants6     }7}

The following decomposition describes the five phases of the Java class life cycle:

Class Loading phase:

1. Obtain a binary byte stream that defines this class by using the fully qualified name of a class
2. Convert the static storage structure represented by this byte stream into the runtime data structure of the method area * *
3. Generate a Java.lang.class object representing this class in the Java heap as the access entry for the data in the method area * *

When the load phase is complete, the binary byte stream outside the virtual machine is stored in the method area in the format required by the JVM, and the data storage format of the method area is defined by the virtual machine implementation, and the JVM specification does not specify the specific data structure for this area. Then instantiate an object of the Java.lang.class class in the Java heap, which will act as the external interface for the program to access these types of data in the method area. The load phase and part of the connection phase (such as some bytecode file format validation actions) are interleaved, and the load phase has not yet completed the connection phase may have started, but the actions taken during the loading phase of these clips are part of the connection phase, which still maintains a fixed sequence of phases.

Validation phase: The first step in the connection phase, which is to ensure that the information contained in the byte stream of the class file conforms to the requirements of the current virtual machine and does not compromise its security.

1. File Format Verification

2. Meta-data validation

3. Byte code Verification

4. Symbol Reference Validation

Preparation phase: The preparation phase is the stage in which the variables are formally allocated memory and set class variable initialization, which are allocated in the method area. At this point the memory-allocated variables include only class variables (static adornments), not instance variables, and instance variables are allocated in the Java heap along with the objects as they are instantiated. The initial value is typically a 0 value of the data type. * * *

Special cases the Constantvalue property is assigned the value of the Constantvalue property, such as public static final int value = 123;

Parsing phase: The JVM replaces symbolic references within a constant pool with direct reference procedures

1. Symbol reference (Symbolic References): the symbol refers to a set of symbols to describe the referenced target, the symbol can be any form of the literal, as long as the use of a non-ambiguous positioning to the target can be. For example, in a Class file it appears as constants of type Constant_class_info, Constant_fieldref_info, Constant_methodref_info, and so on. The symbolic reference is independent of the memory layout of the virtual machine, and the referenced target does not necessarily load into memory. In Java, a Java class will be compiled into a class file. At compile time, the Java class does not know the actual address of the referenced class, so it can only be substituted with symbolic references. The Org.simple.Language class is referenced more than the Org.simple.People class, and at compile time the people class does not know the actual memory address of the Language class, so only the symbolic org.simple.Language is used (assuming this is, of course, actually by a co Nstant_class_info represented) to represent the address of the language class. The memory layouts implemented by various virtual machines may be different, but they can accept a consistent symbol reference because the literal form of the symbol reference is clearly defined in the Java Virtual Machine specification's class file format.

2. Direct reference:

(1) A pointer to the target directly (for example, a direct reference to a "type" "Class object", a class variable, a class method may be a pointer to a method area)

(2) Relative offset (for example, direct reference to instance variable, instance method is offset)

(3) A handle that can be positioned indirectly to the target
Direct references are related to the layout of the virtual machine, and the same symbolic reference is generally not the same as the direct reference translated on different virtual machine instances. If there is a direct reference, the referenced target must already be loaded into memory. ***

Initialization phase: Class initialization method:<clinit> ()

Refreshes the initial values of the preparation phase and initializes class variables and other resources according to the program's subjective plan. The initialization phase is the process of executing the constructor <clinit> () method. The <clinit> () method is generated by the compiler's automatic collection of assignment actions for all class variables in the class and statements in the static statement block (static{} block), which are collected in 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 him, and is assignable 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 explicitly call the parent class constructor, which guarantees that the parent class's < before the subclass's <clinit> () method executes. The Clinit> () method has been executed, so the class of the first executed <clinit> () method in the virtual machine is definitely Java.lang.object because the parent class's <clinit> () method first executes, It also means that the static statement block defined in the parent class takes precedence over the assignment of a variable in the subclass. 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 produce the <clinit> () method for this class. A static statement block cannot be used in an interface, but there is still an assignment operation to initialize the variable, so the interface will produce the <clinit> () method as the 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 variable defined in the parent interface is used.

In addition, the implementation class of the interface does not execute the <clinit> () method of the interface at initialization time. The virtual opportunity guarantees that a class <clinit> () method is correctly locked in a multithreaded environment, and if multiple threads initialize a class at the same time, there will only be one thread executing the class's <clinit> () method, and the other threads will need to block the wait. Until the active thread executes the <clinit> () method is complete. If there is a lengthy operation in the <clinit> () method of a class, it can cause multiple processes to block.

Life cycle of Java classes

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.