1.获取ClassLoader
1 public class MyTest06 { 2 3 public static void main(String[] args) { 4 String str = new String("212"); 5 C c = new C(); 6 System.out.println(str.getClass().getClassLoader()); 7 System.out.println(c.getClass().getClassLoader()); 8 } 9 10 } 11 class C{ 12 13 }//out: null
sun.misc.Launcher$AppClassLoader@2a139a55
第二个返回值说明自定义类C是由AppClassLoader 系统类加载器加载
第一个返回值说明String由Bootstrap根加载器加载
/** 如下是源码中对 getClassLoader() 方法的说明
* Returns the class loader for the class. Some implementations may use
* null to represent the bootstrap class loader. This method will return
* null in such implementations if this class was loaded by the bootstrap
* class loader.
* 如果类由bootstrap class loader加载 这个方法会返回null在一些jvm实现中
*
* <p> If a security manager is present, and the caller‘s class loader is
* not null and the caller‘s class loader is not the same as or an ancestor of
* the class loader for the class whose class loader is requested, 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 class loader for the class.
*
* <p>If this object
* represents a primitive type or void, null is returned.
*
* @return the class loader that loaded the class or interface
* represented by this object.
* @throws SecurityException
* if a security manager exists and its
* {@code checkPermission} method denies
* access to the class loader for the class.
* @see java.lang.ClassLoader
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
*/
2.ClassLoader.loadClass()方法与Class.forName()方法
class CL{
static {
System.out.println("class cl");
}
}
public class MyTest07 {
public static void main(String[] args) throws Exception {
ClassLoader clazzloader = ClassLoader.getSystemClassLoader();
Class<?> clazz = clazzloader.loadClass("jvm.CL");
System.out.println("==================================");
System.out.println(clazz);
System.out.println("==================================");
Class<?> clazz2 = Class.forName("jvm.CL");
System.out.println("==================================");
System.out.println(clazz2);
}
}//out:
==================================
class jvm.CL
==================================
class cl
==================================
class jvm.CL
loadclass 只加载对应类,forName 会一并将类初始化
不过调用 clazz.newInstance()会初始化该类:
public class MyTest07 {
public static void main(String[] args) throws Exception {
ClassLoader clazzloader = ClassLoader.getSystemClassLoader();
Class<?> clazz = clazzloader.loadClass("jvm.CL");
System.out.println("==================================");
System.out.println(clazz);
System.out.println("==================================");
clazz.newInstance();
System.out.println("==================================");
Class<?> clazz2 = Class.forName("jvm.CL");
System.out.println("==================================");
System.out.println(clazz2);
}
}out:
==================================
class jvm.CL
==================================
class cl
==================================
==================================
class jvm.CL
3.
原文:https://www.cnblogs.com/chafanbusi/p/10644492.html