The difference class.forName and classLoader

In Java Class.forName () and can be used for class ClassLoader loaded. Class.forName () In addition to the class .class file is loaded into the JVM will explain the class, static block the implementation of the class, but only ClassLoader to load the .class file into the JVM, will not perform static blocks, only newInstance when it is executed static block. Class.forName (name, initialize, loader) function can also be parameterized control whether static load block. And only call the newInstance () method will call the constructor, create an object class.

Take a look at Class.forName () source code

@CallerSensitive
public static Class the forName (String className) <?>
Throws a ClassNotFoundException {
Class = Caller Reflection.getCallerClass () <?>;
// second parameter to true, indicating whether or not initialized, which is code that performs static
return forName0 (className, to true, ClassLoader.getClassLoader (Caller), Caller);
}

Guess you like

Origin www.cnblogs.com/codlover/p/11571677.html