Why use a class loader?
In Java language, class loading is completed during program operation. Although this strategy will slightly increase performance overhead when class loading, it will provide Java applications with a high degree of flexibility. For example:
1. Write an interface-oriented application and may wait until it runs before specifying its implementation subclass;
2. Users can customize a class loader, allowing the program to load a binary stream from the network or other places as part of the program code at runtime; (This is the basis for Android plug-in, dynamic installation and update of the apk)
Why study the entire process of class loading?
Class loading mechanism
The JVM loads the class file into memory, and checks, parses and initializes the data, and finally forms the entire process of java type that the JVM can directly use.
load
Load the bytecode content of the class file into memory, and convert the static data into a runtime data structure in the method area, and generate a java.lang.Class object representing this class in the heap, as the access portal for the method area class data. This process requires the participation of the class loader.
Link
The process of combining the binary code of the java class into the running state of the JVM
initialization
<clinit>() method. The class constructor <clinit>() method is generated by the compiler automatically collecting the assignment actions of all class variables in the class and the merge of statements in the static statement block (static block).<clinit>() method is correctly locked and synchronized in a multi-threaded environment.Example 1:
public class Demo01 { public static void main(String[] args) { A a = new A(); System.out.println(a.width); }}class A{ public static int width=100; //static variable, static field field static{ System.out.println("static initialization class A"); width = 300; } public A() { System.out.println("Create an object of class A"); }}analyze:
illustrate:
There are stacks, heaps (places created objects), and method areas in memory (actually a special heap)
1. When the JVM loads Demo01, first form the static data (class variables, class methods, code...) in the method area. At the same time, a java.lang.Class object (reflective object) will be formed in the heap, representing the Demo01 class. Through the object, the class binary structure can be accessed. Then load the variable Class A information, and also form an object a in the heap, representing Class A.
2. When the main method is executed, the main method stack frame will be formed in the stack, and one method corresponds to a stack frame. If the main method calls other methods, it will press it one by one in the stack. There is a local variable A type a in the main method. At the beginning, the value of a is null. The constructor of class A is called through new. The A() method is generated in the stack and the A object is generated in the heap. Then the A object address is paid to a in the stack. At this time, a has the A object address.
3. When A.width is called, the method area data is called.
When a class is loaded by reference, the class will only be loaded once
Active reference of the class (class initialization will definitely occur)
java.lang.reflect package method to make reflection calls to the class Passive reference to the class (class initialization will not occur)
Example 2:
public class Demo01 { static{ System.out.println("static initialization Demo01"); } public static void main(String[] args) throws Exception { System.out.println("Demo01's main method!"); System.out.println(System.getProperty("java.class.path")); // Active reference // new A();// System.out.println(A.width);// Class.forName("com.sinosoft.test.A"); // Passive reference // System.out.println(A.MAX);// A[] as = new A[10]; System.out.println(B.width);// Class B will not be loaded}}class B extends A { static { System.out.println("static initialization B"); }}class A extends A_Father { public static int width=100; // Static variable, static domain field public static final int MAX=100; static { System.out.println("static initialization class A"); width=300; } public A(){ System.out.println("Create an object of Class A"); }}class A_Father extends Object { static { System.out.println("Static initialization A_Father"); }}Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.