在Java中Class.forName和ClassLoader都可以对类进行加载。
ClassLoader就是遵循双亲委派模式 最终调用启动类的类加载器。实现的功能是通过一个类的全名来获取此类的二进制字节流。获取后放到JVM中。
Class.forName实际上也是调用的ClassLoader来实现的。
@CallerSensitive
public static Class<?> forName(String className)
throws ClassNotFoundException {
Class<?> caller = Reflection.getCallerClass();
return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
}
从上面的Class.forName(String className)的源码中可以看出,调用的是forName0这个方法,而这个方法的第二个参数默认设置为true,就代表会执行类中的静态代码块,以及对静态变量的赋值等操作。第三个参数就是调用ClassLoader来实现的。
同时也可以调用 Class.forName(String name, boolean initialize, ClassLoader loader)方法来手动选择在加载类的时候是否要对类进行初始化, Class.forName(String name, boolean initialize, ClassLoader loader)的源码如下:
/**
* Returns the {@code Class} object associated with the class or
* interface with the given string name, using the given class loader.
* Given the fully qualified name for a class or interface (in the same
* format returned by {@code getName}) this method attempts to
* locate, load, and link the class or interface. The specified class
* loader is used to load the class or interface. If the parameter
* {@code loader} is null, the class is loaded through the bootstrap
* class loader. The class is initialized only if the
* {@code initialize} parameter is {@code true} and if it has
* not been initialized earlier.
*
* <p> If {@code name} denotes a primitive type or void, an attempt
* will be made to locate a user-defined class in the unnamed package whose
* name is {@code name}. Therefore, this method cannot be used to
* obtain any of the {@code Class} objects representing primitive
* types or void.
*
* <p> If {@code name} denotes an array class, the component type of
* the array class is loaded but not initialized.
*
* <p> For example, in an instance method the expression:
*
* <blockquote>
* {@code Class.forName("Foo")}
* </blockquote>
*
* is equivalent to:
*
* <blockquote>
* {@code Class.forName("Foo", true, this.getClass().getClassLoader())}
* </blockquote>
*
* Note that this method throws errors related to loading, linking or
* initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
* Java Language Specification</em>.
* Note that this method does not check whether the requested class
* is accessible to its caller.
*
* <p> If the {@code loader} is {@code null}, and a security
* manager is present, and the caller's class loader is not null, then this
* method calls the security manager's {@code checkPermission} method
* with a {@code RuntimePermission("getClassLoader")} permission to
* ensure it's ok to access the bootstrap class loader.
*
* @param name fully qualified name of the desired class
* @param initialize if {@code true} the class will be initialized.
* See Section 12.4 of <em>The Java Language Specification</em>.
* @param loader class loader from which the class must be loaded
* @return class object representing the desired class
*
* @exception LinkageError if the linkage fails
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails
* @exception ClassNotFoundException if the class cannot be located by
* the specified class loader
*
* @see java.lang.Class#forName(String)
* @see java.lang.ClassLoader
* @since 1.2
*/
@CallerSensitive
public static Class<?> forName(String name, boolean initialize,
ClassLoader loader)
throws ClassNotFoundException
{
Class<?> caller = null;
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// Reflective call to get caller class is only needed if a security manager
// is present. Avoid the overhead of making this call otherwise.
caller = Reflection.getCallerClass();
if (sun.misc.VM.isSystemDomainLoader(loader)) {
ClassLoader ccl = ClassLoader.getClassLoader(caller);
if (!sun.misc.VM.isSystemDomainLoader(ccl)) {
sm.checkPermission(
SecurityConstants.GET_CLASSLOADER_PERMISSION);
}
}
}
return forName0(name, initialize, loader, caller);
}
if {@code true} the class will be initialized.意思就是:如果initialize为true,就进行类的初始化。
接下来实际动手测试一下:Class.forName
public class ReflectTest {
@Test
public void test01() {
try {
Class.forName("com.hucl.sell.littleDemo.ReflectDemo");
System.out.println("--------------Class.forName()反射产生的结果-----------------");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
运行结果如下:
This is static code
execute this static method
--------------Class.forName()反射产生的结果-----------------
ClassLoader:
@Test
public void test02() {
try {
ClassLoader.getSystemClassLoader().loadClass("com.hucl.sell.littleDemo.ReflectDemo");
System.out.println("--------------ClassLoader反射产生的结果-----------------");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
结果:
--------------ClassLoader反射产生的结果-----------------
可以看出ClassLoader对静态代码块和静态变量并没有进行初始化。
所以Class.forName除了将类的.class文件加载到jvm中之外,还会对类进行解释,执行类中的static块。
而classloader只干一件事情,就是将.class文件加载到jvm中,不会执行static中的内容,只有在newInstance才会去执行static块。
Class.forName(name,initialize,loader)带参数也可控制是否加载static块。并且只有调用了newInstance()方法采用调用构造函数,创建类的对象。