class loading

What is class loading?

Class loading refers to reading the binary data in the .class file of the class into memory, placing it in the method area of ​​the runtime data area, and then creating a java.lang.Class object in the heap area for Encapsulates the data structure of the class within the method area. The final product of class loading is the Class object located in the heap area. The Class object encapsulates the data structure of the class in the method area and provides Java programmers with an interface to access the data structure in the method area.


There are three ways of class loading:

When the command line starts the application, it is initialized and loaded by the JVM
. Dynamically loaded through the Class.forName() method. Dynamically loaded
through the ClassLoader.loadClass() method.

Class.forName()和ClassLoader.loadClass()区别

Class.forName(): In addition to loading the .class file of the class into the jvm, it will also interpret the class and execute the static block in the class;
ClassLoader.loadClass(): Only do one thing, that is, load the .class file When loaded into the jvm, the content in static will not be executed, and the static block will only be executed in newInstance.
Note:
The Class.forName(name, initialize, loader) function with parameters can also control whether to load the static block. And only when the newInstance() method is called, the constructor is called to create an object of the class





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325955881&siteId=291194637