The initialization phase is the process of executing the class constructor < clinit > () method, which is generated by the compiler automatically collecting the copy action of all class variables in the class and the statement merging in the static block
When initializing a class, if it is found that its parent class has not been initialized, the initialization of its parent class needs to be triggered first
When accessing a static domain of a Java class, only the class that actually declares the domain will be initialized
public class Demo01 { static{ System.out.println("initiate static Demo01"); } public static void main(String[] args) throws Exception { System.out.println("Demo01 Of main Method!"); System.out.println(System.getProperty("java.class.path")); //Active citation new A(); // System.out.println(A.width); // Class.forName("com.bjsxt.test.A"); //Passive citation // System.out.println(A.MAX); // A[] as = new A[10]; // System.out.println(B.width); } } class B extends A { static { System.out.println("initiate static B"); } } class A extends A_Father { public static int width=100; //Static variable, static field public static final int MAX=100; static { System.out.println("Static initialization class A"); width=300; } public A(){ System.out.println("Establish A Class objects"); } } class A_Father extends Object { static { System.out.println("initiate static A_Father"); } }
- Active reference of class (class initialization must occur)
– new an object of a class
– calling static members (except for final constants) and static methods of a class
– use the method of java.lang.reflect package to make a reflection call on the class
- when the virtual machine starts, java Hello, the Hello class must be initialized. To put it bluntly, start the class where the main method is located
- when initializing a class, if its parent class is not initialized, its parent class will be initialized first
- Passive reference to class (class initialization does not occur)
- when accessing a static domain, only the classes that actually declare the domain will be initialized
– static variables of the parent class are referenced through the child class, which will not cause the child class to initialize
- class reference is defined by array, and class initialization will not be triggered
- Reference constants do not trigger the initialization of this class (constants are stored in the constant pool of the calling class during the compilation phase)
Virtual opportunity ensures that the < clinit > () method of a class is locked and synchronized correctly in a multithreaded environment
The design mode of Han Dynasty in single design mode:
1. Private construction method (to ensure that objects cannot be created outside)
2. Declare the reference type variable of this class, and create the object (ensure that the object creates itself)
3. Provide a static method to ensure that the external can be called by different objects (must be public)
//Design mode of hungry man public class Single { //Privatization structure private Single(){} //Create this type of object //When the hungry Chinese class is actively referenced, initializing and building the object can ensure that only one object is created, even in the multithreaded environment private static Single single = new Single(); //Provide a common way public static Single getInstance() { return single; } }
Lazy design mode of single design mode:
1. Private construction method (to ensure that objects cannot be created outside)
2. Declare the reference type variable of this class, but do not create the object first
3. Provide a static method to ensure that the external can be called by different objects, but only when the method is called can the object be created
//A single design mode for lazy people class Single02{ //Privatization structure private Single02(){} //Create this type of object private static Single02 single; public static Single02 getInstance() { if(single == null) { single = new Single02(); } return single; } }
The starving class is instantiated when it is loaded. Even if thread1 and thread2 get it at the same time, they get the value of the variable instantiated when the class is loaded, so it is thread safe. The lazy thread is not safe, because it is possible that thread1 enters the if body when the if (instance==null) is judged to be true, but it is not instantiated. At this time, thread2 also comes in There will be two examples.