Explanation
Class in java. Both forName() and lassLoader can load classes. ClassLoader is a class loader that follows the parental delegation model and ultimately calls the startup class loader. It implements the function of "getting a binary byte stream describing this class by its fully qualified name", getting it into the binary stream and putting it into the JVM. Class. The forName() method is actually implemented by calling LassLoader.
Class.forName(String className); The source code for this method is
@CallerSensitive public static Class<?> forName(String className) throws ClassNotFoundException { Class<?> caller = Reflection.getCallerClass(); return forName0(className, true, ClassLoader.getClassLoader(caller), caller); }
The second parameter is true, indicating whether to initialize or not, and the default is initialization. Once initialized, the initialization of the object's static code block and static parameters will start.
Class initialization refer to: My class loading mechanism blog
ClassLoader in JDK8. LoadClass() Source
public Class<?> loadClass(String name) throws ClassNotFoundException { return loadClass(name, false); }
ClassLoader.loadClass(className) method, the method actually called internally is loadClass(name, false);
The second boolean parameter indicates whether the target object is linked or not, and false indicates that the static block and the static object will not be executed without linking, that is, without some column steps including initialization.
Give an example
//Static Code Block static { System.out.println("Executed static code block"); } //Static variable private static String staticFiled = staticMethod(); //Static methods for assigning static variables public static String staticMethod(){ System.out.println("Executed static method"); return "Assigned static fields"; }
Verification:
The difference between them is that one initializes the operation, the other does not, while initialization is primarily used to execute static blocks of code during class loading
public class Test { //Static Code Block static { System.out.println("Executed static code block"); } //Static variable private static String staticFiled = staticMethod(); //Static methods for assigning static variables public static String staticMethod(){ System.out.println("Executed static method"); return "Assigned static fields"; } }
Test:
public class Test1 { public static void main(String[] args) throws ClassNotFoundException { Class aClass = Class.forName("com.neutech.refleaction.demo07.Test"); System.out.println("============="); ClassLoader.getSystemClassLoader().loadClass("com.neutech.refleaction.demo07.Test"); } }
Output results:
Executed static code block
Executed static method
=============
Conclusion: Class is derived from the results of operation. While forName loads the class into initialization (executing static code blocks), ClassLoader's loadClass does not initialize the class but loads it into the virtual machine.
Application Scenarios
The implementation of IOC in the Spring framework that we are familiar with is using ClassLoader.
Class is usually used when we use JDBC. The forName() method loads the database connection driver. This is because the JDBC specification explicitly requires Driver (database-driven) classes to register themselves with DriverManager. (This is essentially to call the registration function in the static initialization block when loading the class)
Take the drive for MySQL as an example:
public class Driver extends NonRegisteringDriver implements java.sql.Driver { // // Register ourselves with the DriverManager // static { try { java.sql.DriverManager.registerDriver(new Driver()); } catch (SQLException E) { throw new RuntimeException("Can't register driver!"); } } /** * Construct a new driver and register it with DriverManager * * @throws SQLException * if a database error occurs. */ public Driver() throws SQLException { // Required for Class.forName().newInstance() } }
* We see that the actions that Driver registers with DriverManager are written in the static code block, which is why Class is used when writing JDBC. For Name(). (
These are personal notes. If there are any errors, you are welcome to criticize and correct them.