JVM(三、雙親委派機制1)
阿新 • • 發佈:2019-04-02
pan sent adc urn requested turn returns spa true
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
[email protected]
第二個返回值說明自定義類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.
JVM(三、雙親委派機制1)