The class bytecode file is loaded into memory by the JVM virtual machine, followed by
- Connection
- Verification: Byte code verification
- Prepare: Allocates memory for static variables and assigns default values for variable types (note: The base type is 0 or false, and the object is directly assigned the corresponding value for the null,static final modifier)
- Parse: A symbol reference in a class is converted to a direct reference
- Initialize: initializes the corresponding value for the static variable/static statement block of the class
and the class initialization opportunity is: class in the case of the first active use , the active use includes the following 6 cases:
- When instantiating an object with the New keyword.
- When a static method of a class is called.
- When reading or setting a static field for a class (except when it is final decorated, the result is placed in a constant pool at compile time).
- Use the Java.lang.reflect package method to make reflection calls to the class.
- When a subclass of a class is initialized.
- When the virtual machine starts, it is marked as the startup class (the class that contains the main method).
From above we know that the initialization of a static member variable occurs during the initialization phase of the class, and to instantiate an object you must first load, concatenate, and initialize the class before you can use it to instantiate the object, not the
The initialization of static member variables occurs during the instantiation of an object. The following example shows that when a class is first actively using static blocks or static member variables, it is initialized.
1 package com.space.equalstest; 2 3 public class Dog {4 public static String name = "Tom"; 5 6 static {7 System.out.println ("Dog INI T "); 8 } 9 public static void info () { System.out.println ("a Dog"); }13}
1 Package com.space.equalstest;2 3 public class Test {4 public static void Main (string[] args) {5 dog.info ();//First The active use is initialized with 6 dog.info ();//No more initialization of 7 }8}
Output Result:
Static member variables for parsing Java classes are initialized before non-static member variables