Using a class is something that happens
Load, link (validate, prepare, parse), initialization, and so on, can be used
load: load the class file bytecode content into memory and convert the static data into a run-time data structure in the method area
A Java.lang.Class object representing this class is generated in the heap as the access entry for the method area class data.
Link: The process of merging the Java class's binary code into the running state of the JVM
Validation: Ensure that the loaded class information conforms to the JVM's specifications and there are no security issues. (Ensure that the loaded binaries do not corrupt the JVM).
Prepare: The stage for allocating memory and setting the initial value of a variable for a class variable (static variable, which is initialized to 0 at this time, the user's initialization value is assigned to the class variable at subsequent initialization), and the memory is allocated in the method area.
Parse: A symbol reference in a virtual machine constant pool is replaced by a direct reference procedure. (Note: The contents of a constant pool include the class name, variable names inside the class, string constants, and so on)
Initialization
The initialization phase is the process of executing the class constructor <clinit> () method, and the class constructor <clinit> () method is generated by the compiler's automatic collection of assignment actions for all class variables in the class and statement merges in static statement blocks (static blocks) (in short, is to merge static variables and static code blocks of the class to form a class constructor for execution).
When initializing a class, it is necessary to trigger the initialization of its parent class if it finds that its parent class has not yet been initialized.
Virtual opportunities Ensure that the <clinit> () method of a class is properly locked and synchronized in a multithreaded environment.
When accessing a static domain of a Java class, only the classes that actually declare the domain are initialized.
The following code illustrates the process of executing the class constructor <clinit> () during the initialization phase, where the class constructor is executed prior to the constructor of the class.
Class A {public static int width = 100;static {System.out.println ("before Static initialization class A" +a.width); width = 300; System.out.println ("After static initialization of class A" +a.width); Public A () {System.out.println ("Create object of Class A");}} public class Demo {public static void main (string[] args) {A A = new A (); System.out.println (A.width);}}
You can see the execution of the class construction method <clinit> () before the constructor.
The following code shows that when a class is initially initialized, it is necessary to trigger the initialization of its parent class if it finds that its parent class has not yet been initialized.
Package Com.test;class a_father{static {System.out.println ("Static initialization A_father");}} Class A extends a_father{public static int width = 100;static {System.out.println ("before Static initialization class A" +a.width); width = 300; System.out.println ("After static initialization of class A" +a.width); Public A () {System.out.println ("Create object of Class A");}} public class Demo {public static void main (string[] args) {A A = new A (); System.out.println (A.width);}}
Look at the following code analysis output
Package Com.test;class a_father{static {System.out.println ("Static initialization A_father");}} Class A extends a_father{public static int width = 100;static {System.out.println ("before Static initialization class A" +a.width); width = 300; System.out.println ("After static initialization of class A" +a.width); Public A () {System.out.println ("Create object of Class A");}} public class Demo {static{system.out.println ("Static Initialization of Demo");} public static void Main (string[] args) {System.out.println ("Main method of demo"); A = new A (); System.out.println (A.width); A a2 = new A ();//java.lang.class<a> AClass = A.class;//class aclass2 = A.class;}}
The output is as follows:
Static initialization of Demo
The Main method of the demo
Static initialization of A_father
Before static initialization class A100
After static initialization class A300
Create an object of Class A
300
Create an object of Class A
The first program to load the public demo class, Initialize the class (execution class construction method) and then print the "static initialization of the demo", the class initialization after the completion of the method call, that is, the main method, the output "Demo Main method", at this time to execute the next sentence a = new A (); Because Class A is not loaded at this time, so the first thing to load Class A, and then link initialization, the initialization of a also found that a parent class is not loaded, and then to load the parent class, link to initialize the parent class, print the message "Static initialization A_father", the parent class initialization is complete, then the initialization of Class A begins, then "Before Static initialization class A100" and "after static initialization class A300", by this time, the class has all loaded into memory, so can create an object, then print "Create Class A object", execute the PRINT statement output "300", Finally, an object of a is created, but because the class has already been loaded, only the constructor of the class is called to "create an object of Class A", which shows that the class is loaded only once, and the instantiation of the class can be multiple times.
There is one detail about the loading of classes:
Classes are loaded into classes that are passive references and classes are active references, and the class's passive reference does not occur when the class is initialized, and the class's active reference must occur.
a passive reference to a class is :
1. When accessing a static domain, only classes that actually declare the domain are initialized (referencing a static variable of the parent class through a subclass does not cause the subclass to initialize, only the parent class is initialized)
2. Defining a class reference through an array does not trigger initialization of this class.
3. Reference constants do not trigger initialization of this class (constants are stored in the constant pool of the calling class during the compilation phase)
the active references to the class are :
1.new an object of a class
2. Call the static member of the class amount (except the final constant) and the static method.
3. Using the Java.lang.reflect package method to make a reflection call to a class
4. When the virtual machine is started, Java Hello is bound to initialize the Hello Class (which is straightforward to first load the class where the Main method resides)
5. When a class is initialized, its parent class is initialized first if its parent class is not initialized.
Test code:
Package Com.test;class a_father{static {System.out.println ("Static initialization A_father");}} Class A extends a_father{public static int width = 100;public static final int MAX = 100;static {System.out.println ("befor e static initialization of class A "+a.width"; width = 300; System.out.println ("After static initialization of class A" +a.width); public static void Test () {System.out.println ("static method"); Public A () {System.out.println ("Create object of Class A");}} Class B extends A{static {System.out.println ("Static initialization B");}} public class Demo {static{system.out.println ("Static Initialization of Demo");} public static void Main (string[] args) throws ClassNotFoundException {System.out.println ("Main method of Demo");//Active load//new A ( );//system.out.println (a.width);//a.test ();//class.forname ("com.test.a");//Passive load//system.out.println (A.MAX);//A [] as = new A[10]; System.out.println (B.width);}}
Java class loading mechanism