Class initialization

Active reference [meeting initiation]

  • 1. Instantiate the object with the New keyword.
  • Access or assign a value to a static variable of a class or interface.
  • 3. Call the static method of the class.
  • 4. When a class is called by reflection using a method in the java.lang.Reflect package
  • 5. When initializing a subclass, if the parent class is not initialized, initialize the parent class first
  • 6. When starting a VM, you need to specify a primary class, such as the Main method class. The VM preferentially initializes this primary class.
  • 7, JDK1.7 began to provide dynamic language support, when the Java. Lang. Invoke. After MethodHandler instance as a result of the REF – getStatic/REF_putstatic/REF_invokeStatic handle, And the classes corresponding to these handles should be initialized first if they are not.

Summary: Active use of a class or interface is only considered if the static variable or method accessed by a program is actually defined in the current class or interface

Passive reference [does not cause initialization]

  • 1. Using a subclass to refer to a static field of a parent class only triggers initialization of the parent class, not the subclass.
  • 2. Referencing a class through an array definition does not trigger initialization of the class. SuperClass[] superClasses = new SuperClass[10]
  • 3. Referring to a static constant of a class does not trigger initialization because the constant is confirmed at compile time. public static final int value = 22;

Interface initialization

  • 1. When a Java virtual machine initializes a class, it requires that all of its parent classes have been initialized, but this rule does not apply to interfaces.
  • 2. When initializing a class, the interface it implements is not initialized first.
  • When an interface is initialized, its parent class is not initialized first.
  • 4. A superclass interface is not initialized by its subclass or implementation class. It is initialized only when a program first uses a static variable of a particular interface.

Class life cycle

loading

Gets the binary byte stream of a class by a class fully qualified name. Transform the static storage structure represented by this byte stream into the runtime data structure of the method area. Generate a java.lang.Class object representing a Class in memory as a method area for the Class’s various data access points.


How are static variables loaded?

  • GetStatic Gets the static variable of the class.
  • PutStatic Assigns a value to a class static variable.
  • InvokeStatic invokes static methods of the class.

To find and load binary data for a class, you read binary Java types into the Java Virtual machine.

The connection

validation

  • File format verification: Whether the byte stream complies with the class file format specification.
  • Metadata validation: Compliance with Java language syntax specifications.
  • Bytecode verification: The method body is checked and analyzed to ensure correct operation.
  • Symbol reference verification: Check the matching of symbol reference information in the constant pool.

To prepare

In the preparation phase, the Java virtual machine allocates memory for static variables of the class and initializes them with default values. Allocates memory for class variables, sets default values, but does not initialize them to true values until initialization is reached. For example, for the following Fruit class, in the preparation phase, 4 bytes of memory will be allocated for static variable A of type int, with a default value of 0, and 8 bytes of memory will be allocated for static variable B of type long, with a default value of 0.

[Snippet]class Fruit{
    public static int a = 1;
    public static long b;
    public static long c;
 static {  b = 2L;  } } Copy the code

parsing

To convert symbolic references in a class into direct references, parsing is the process of looking for symbolic references to classes, interfaces, fields, and methods in the constant pool of a type and replacing them with direct references.

Initialize the

A static code block initializes a static variable of a class, assigning the class variable the correct initial value.

During the initialization phase, the Java virtual machine executes the initialization statement of the class, assigning initial values to the static variables of the class. In a program, static variables can be initialized in two ways :(1) at the declaration of a static variable, and (2) in a static code block. For example, in [the code snippet above], static variables A and B are displayed initialized, while static variable C is not displayed initialized, which will keep its default value of 0.

Declarations of static variables, as well as static code blocks, are treated as class initializers. The Java virtual machine executes the initializers in the order in the class file. For example, public static int a = 6; After initialization, this static variable a has a value of 4.


Class initialization steps

  1. If the class has not already been loaded and wired, do so first.
  2. If the class has a direct parent that has not already been initialized, initialize the immediate parent first.
  3. If there are initializers in the class, execute those initializers in turn.

use

uninstall

Clears instances of the class.

Clears the ClassLoader reference to the class.

Clears a reference to the class object

For example, 🌰


  • Static int I = 100 public static int I = 100
  • A: 1. Allocate memory in the preparation phase and set the initial value to 0. 2.

How the Class object is obtained and whether it causes initialization

  • Class clazz1 = Class name. Class has not completed initialization.
  • Class clazz2 = Class name.class.getClassLoader ().loadClass(” fully qualified name “); The initialization process is not complete.
  • Class clazz3 = class.forname (” fully qualified name “) completes the initialization process.
  • Class clazz4 = object reference. GetClass (); The object exists and the initialization process is complete.

The difference between the four methods is that there is no initialization.

Mind maps

Hey, big guy, you can see this. NoFocus onOne more go 🍻🍻