Classloading initialization

Initialization phase is the implementation class constructor <clinit> () method, the class constructor <clinit> () method is a copying operation by the compiler automatically collected class all class variables and static block of statements (static block) in statement of merger

When initializing a class, if you find the parent class has not been initialized, you need to trigger initialization of the parent class

When accessing a Java class static field, only the real statement of this field will be initialized class

public class Demo01 {
	static{
		System.out.println("静态初始化Demo01");
	}
	
	
	public static void main(String[] args) throws Exception {
		System.out.println("Demo01的main方法!");
		System.out.println(System.getProperty("java.class.path"));
		
		//主动引用
		new A();
//		System.out.println(A.width);
//		Class.forName("com.bjsxt.test.A");
		
		
		//被动引用
//		System.out.println(A.MAX);
//		A[] as = new A[10];
//		System.out.println(B.width);
		
	}
}

class B  extends A {
	static {
		System.out.println("静态初始化B");
	}
}

class A extends A_Father {
	public static int width=100;   //静态变量,静态域    field
	public static final  int MAX=100; 
	
	static {
		System.out.println("静态初始化类A");
		width=300;
	}
	public A(){
		System.out.println("创建A类的对象");
	}
}

class A_Father extends Object {
	static {
		System.out.println("静态初始化A_Father");
	}
}
  • Active reference to the class (the class initialization will occur)

- a new class of objects
- call the static member class (except final constants) and static methods
- using java.lang.reflect package approach to reflect the class call
- when the virtual machine is powered on, java Hello, the class will be initialized Hello . It means first start the class where the main method
- the father of his class when a class is initialized, if the parent class is not initialized, the first initializes

  • Passive reference to the class (class initialization does not happen)

- When accessing a static field, just like a real statement of this field will be initialized, that reference a static variable through the parent class subclass does not lead to a subclass initialization
- referenced by an array of custom class, this class does not trigger initialization
- constant references will not trigger such initialization (constants at compile it into the constant pool of the class calls)

 

Virtual opportunity to ensure that a class <clinit> () method is properly locked and synchronized in a multithreaded environment

Singleton design pattern of starving design pattern:

1. private constructor (to ensure that the outside world can not create object)

2. declare a reference type variable of this class, and create an object (the object to ensure that you have created your own)

3. Provide a static method to ensure that the outside world through different objects can be invoked (must be public)

//饿汉设计模式

public class Single {

//私有化构造

private Single(){}

//创建本类对象
//当饿汉式类被主动引用时,初始化并构建对象,即使是多线程环境下<clinit>()方法也能保证只有一个对象被创建

private static Single single = new Single();

//提供一个公共的方法方式

public static Single getInstance() {

return single;

}

}

 

Singleton design pattern the design pattern lazy:

1. private constructor (to ensure that the outside world can not create object)

2. declare a reference type variable in this class, but to not create object

3. Provide a static method to ensure that the outside world can be different over the object to make the call, but must call the method when it is going to create an object

//懒汉单例设计模式

class Single02{

//私有化构造

private Single02(){}

//创建本类对象

private static Single02 single;

public static Single02 getInstance() {

if(single == null) {

single = new Single02();

}

return single;

}

}

Hungry Chinese-style classes at load time to instantiate, even if thread1 and thread2 get it at the same time, to take the example of the value of that variable when the class is loaded, it is said to be thread-safe; thread unsafe and lazy man , because it is possible in thread1 if (instance == null) determines the true if entered in the body, but not any start instantiated, but this time also came thread2, eventually there will be two instances of.

Published an original article · won praise 0 · Views 26

Guess you like

Origin blog.csdn.net/qq_38374633/article/details/104033818