When it comes to reflection, first say the ClassLoader of the class.
Classes are loaded:
When a program wants to use a class, if the class has not yet been loaded into memory, the system initializes the class by loading, connecting, and initializing three steps.
load :
This means reading the class file into memory and creating a class object for it.
When any class is used, the system creates a class object.
Connection :
Verify that you have the correct internal structure and that it is consistent with other classes
Prepare to allocate memory for static members of the class and set default initialization values
Resolve to replace a symbolic reference in a class's binary data with a direct reference
Initialize :
Is the initial steps we've talked about before.
Class initialization time:
- To create an instance of a class
- Access static variables for a class, or assign a value to a static variable
- To invoke a static method of a class
- Use reflection to force the creation of a Java.lang.Class object for a class or interface
- Initializes a subclass of a class
- To run a main class directly using the Java.exe command
Class Loader:
responsible for loading the. class file into the inside and generating the corresponding class object for it . Although we do not need to care about the class loading mechanism, we understand this mechanism and we can better understand the operation of the program.
The composition of the class loader:
- Bootstrap ClassLoader Root class Loader
- Extension ClassLoader Extension Class loader
- Sysetm ClassLoader System Class Loader
The role of the class loader:
A:Bootstrap ClassLoader root class loader
Also known as the Boot class loader, which is responsible for loading Java core classes
such as system,string and so on. In the Rt.jar file under the Lib directory of the JRE in the JDK
B:Extension ClassLoader Extension class loader
Responsible for loading the jar packages in the extended directory of the JRE.
The Lib directory of the JRE in the JDK under the EXT directory
C:Sysetm ClassLoader System class loader
Responsible for loading the class file from the Java command when the JVM starts, and the jar package and classpath specified by the CLASSPATH environment variable
With these descriptions we can know who is doing the loading of the things we use. So far we've learned to load the class file into memory, so how do we use the contents of these class files if we just stand in the angle of these class files?
Loaders such as Java 27-1 reflection