Static variables for all instances of the same class are shared with a block of memory.
However, data is not shared between two JVMs. After the class is loaded, the system generates a corresponding class object for it.
1. Loading of classes
Reads the class file of the classes into memory and creates a class object.
2. Connect
The process of connecting is divided into three steps:
(1) Verification
The internal structure of the loaded class is correct and coordinated.
(2) Prepare
Allocates memory for class variables and sets default initial values.
(3) Analysis
Replaces a symbolic reference in the binary data of a class with a direct reference.
3. Initialization
The virtual machine is responsible for initializing the class, primarily initializing the class variables.
1. Specify an initial value when declaring a class variable.
2. Use static initialization blocks as class variables.
The JVM Initializes a class that consists of the following steps:
1. If the class has not been loaded and connected, load and connect the class first.
2 If the immediate parent class of the class has not been initialized, its immediate parent class is initialized first.
So when will the system initialize the class?
When the Java program first uses the class or interface in the following 6 ways, the system initializes the class and interface.
(1) Create an instance of the class, including the new operator, to create an instance, and to create the inverse sequence by reflection.
(2) Call a class method of a class (static method)
(3) A class variable that accesses a class or interface or assigns a value to that class variable.
Note: For a final type class variable, the value of the class variable can be determined at compile time. does not cause initialization of the class.
Loading, joining, initializing of classes in Java