Types of ClassLoader:
1.Bootstrap ClassLoader: Responsible for loading JAVA core classes, i.e., $java_home/jre/lib/rt.jar, implemented by C + +
2.Extension ClassLoader: Some jars responsible for loading JAVA platform extensions, including $java_home/jre/lib/*.jar and $java_home/jre/lib/ext/*.jar
3.System (APP) ClassLoader: Responsible for loading the jar or. Class specified in the Classpath
4.Custom ClassLoader: The ClassLoader that the application customizes according to its own needs, such as Tomcat and JBoss, are implemented according to the Java EE specification ClassLoader
The load process checks to see if the class is loaded, the check order is bottom-up, and the custom ClassLoader to bootstrap ClassLoader-by-layer check, as long as a certain classloader is loaded as if the class has been loaded. Ensure that this class is loaded only once by all ClassLoader. The order of loading is top-down, that is, the upper layer tries to load the class one at a level.
The process of class loading:
Loading: Loading the. class file into memory and generating a Java.lang.Class object for it
Second, Link:
1. Validation: Verify that the class being loaded has the correct internal structure and is consistent with other classes
2. Prepare: Allocate memory space for class properties and assign default values
3. Parsing: Converting a symbolic reference in a binary file to a direct reference
Third, initialization: into the initialization phase, the JVM is responsible for initializing the class, that is, to initialize the static properties. There are two ways to initialize a static property: Specify an initial value when declaring a static property, or specify an initial value in a static initialization block
Steps that are included in class initialization
1. If this class has not been loaded and linked, the program first loads the link to the class
2. If the immediate parent class of the class has not been initialized, first initialize its immediate parent class
3. If there are initialization statements in the class, the system executes the initialization statements sequentially
When performing to the second step, the system initializes the immediate parent class with these three steps, and so on, the class that the JVM initializes first is always the Java.lang.Object class, and when the program initializes a class, the system guarantees that the class and all its parent classes are initialized.
Class initialization time:
1. When instantiating a class using the New keyword, when you use reflection to create an instance of the class object corresponding to the classes, when using deserialization to create an object
2. When invoking a static method of a class
3. Except when using static properties of a class (final static), because such properties are directly replaced with actual values at compile time
4. When the subclass is initialized, all the parent classes of the child class are initialized
5. When using reflection to force the creation of a class or interface-corresponding class object
6. When running the main class using the Java.exe command, the JVM initializes the main class first
What is reflection, what function can the reflection mechanism achieve?
Reflection in Java refers to classes or interfaces that can be loaded at run time to detect and use a completely unknown compilation period
Gets the information of the class, dynamically creates the object of the class, sets the value of the member variable, invokes the method, and dynamically creates an access array
Methods for getting class objects in Java: Class name. class, Object. GetClass (), Class.forName method
The class GetFields (), GetMethods (), GetConstructors () methods are the same as Getdeclaredfields (), Getdeclaredmethods (), The difference between the Getdeclaredconstructors method is that the previous method can only get public adornments, and the latter can get all the access control modifiers.
Four ways to create objects in Java are: New reflection Clone Anti-object serialization
Two ways to create objects by reflection
1. Use the Newinstance () method of the class object to create an instance of the class object counterpart. This approach requires that class object classes have a default constructor, and when the Newinstance () method is executed, the default constructor is actually called to create the instance
2. Use the class object to get the corresponding constructor object, and then call the Newinstance () method of the constructor object to create an instance of the class object
And look back on the 22nd day of Java