JAVA-class loading process (novice)

Let's first look at a picture, a personal summary of the class loading process: sorry for the ugly picture.

Through this picture, let's first have a general understanding of the process of class loading. Let's explain it from top to bottom.

1. Load:

Loading is a phase of the class loading process that accomplishes the following three things.

1) Get the binary byte stream of the class through the fully qualified name of the class

2) Convert the static storage structure represented by the binary byte stream into the runtime data structure of the method area.

3) The Class object representing the class is generated in the memory as the access entry of the class

2. Verify:

Verification In order to ensure that the information in the Class file meets the requirements of the virtual machine and does not compromise the security of the JVM.

1) Text format validation.

2) Metadata validation

3) Bytecode Verification

4) Symbolic reference verification

3. Preparation:

The preparation phase is the phase of formally allocating memory for class variables and setting the initial values ​​of class variables, which will be allocated in the method area.

Notice:

  • The variables for memory allocation at this time only include static variables
  • The value to be initialized at this time refers to the zero value of the data variable.

    For example, public static int value =123;
    the value assigned to this variable in the preparation phase is 0, not 123, because no java method is executed at this time.
    If there is a ConstantValue property, it will be initialized according to the value of this property,
    ConstantValue refers to is a variable modified by static final.

4. Analysis:

Replace symbolic references with direct references.

Symbolic references: the target of a reference is described by a symbol

例: String A = "abc";
A就是ABC的符号引用

Quote directly:

直接引用可以使指向目标的指针,偏移量或者能定位到目标的句柄.

此时A会被替换成 0x0000XXX的内存地址

5. Initialization

The java code defined in the class is actually executed during the initialization phase, which is the process of executing the class constructor.

The JVM will ensure that the execution of the superclass has been completed before the subclass calls the clinit method to execute.

The parent class static block takes precedence over the child class.

Summarize:

The point to note is that the two initialization processes should not be confused.

In the initialization phase of the preparation phase, only memory space is allocated for static variables and zero values ​​are assigned.

The java code is actually executed during the initialization phase.

So the initialization sequence for class loading:

1. Allocate memory space for static variables and assign zero values.

2. Parent class static member, method execution

3. Subclass static members, method execution.

4. Parent class code blocks, member variables, constructors

5. Subclass code blocks, member variables, constructors.

Or remember the priority:

static > non-static

parent class > child class

Constructors have the lowest priority at the same level.

Execution order from top to bottom

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644437&siteId=291194637