Introduction to the

This article covers class loading

Supporting the video tutorial: www.bilibili.com/video/BV173… (Updates may be late but not absent)

What is class loading

The Java VIRTUAL machine loads the data describing the Class from the Class file to the memory, verifies, transforms, and initializes the data, and finally forms Java types that can be directly used by the VIRTUAL machine. This process is called the Class loading mechanism of the VIRTUAL machine.

Class loading phase

loading

“Load” is the first step in class loading and performs the following operations

  • Read a class file into memory (i.e. read a stream)

  • Parse this file and generate the corresponding instaceKlass object in c++ (instanceKlass is stored in the method area)

  • Generate instanceMirrorKlass and save it to the heap

Note: The loading process does not specify where to load the class file, so it can be dynamically generated from jar, WAR, or database.

validation

The bottom line is to verify that the current file complies with the Java Virtual machine specification and can be loaded by the current version of the JVM

To prepare

Assign a zero value to a static variable

Assign zero to a variable that is static

Zero: Is not the true value assigned to the variable in the code and can be understood as the initial value.

There is no need to elaborate on what the various types of zeros are.

public class Test {

  // At this stage, I is not assigned 10, but 0 (int zero).
  public static int i = 10;

  // Do not assign a zero value to a because it is an instance variable
  public int a = 2;
  
}
Copy the code

parsing

The parsing phase resolves symbolic references into direct references

Symbolic reference, what is a direct reference?

We now know that Java files are compiled by Javac into class bytecode before being loaded by the JVM

Can we assume that the class file contains the code information for all Java files? (Of course it is), and with that acknowledged, let’s move on

Prepare a class

public class Test {
    HH h = new HH();
}
Copy the code

View the class file after compiling (hexadecimal)

Look at this paragraph

63 6f6d 2f68 616f 7a69 2f48 483b 0100 0a53 6f75 7263 6546 696c 6501 0007 4848

ASCII to a string just like com haozi HH, that means that in the class file HH is still represented as a string and this string we can think of as a symbol of HH, that’s a symbol reference. A direct reference replaces this string with the memory address that HH actually holds

Initialize the

  • Perform clinit

Clinit is compiler generated for merging, all class variables, and statements in static

details

  • Clinit is thread-safe and locks on JVM execution.

  • A problems?

public class Parent {

    static int i = 1;

    static {
        System.out.println("parent"); }}Copy the code
* /public class Son extends Parent {

    static {
        System.out.println("Son"); }}Copy the code
public class Test {
    public static void main(String[] args) {
      inti = Son.i; }}Copy the code

What is the result?


The result is that the console prints parent, and since son. I is parent’s class variable, parent’s Clinit method is initialized to execute the static code block.