Java.lang.Class.getClassLoader() 方法
描述
java.lang.Class.getClassLoader() 方法返回类的类加载器。 一些实现可能使用 null 来表示引导类加载器。 如果此类由引导类加载器加载,则该方法将在此类实现中返回 null。
声明
以下是 java.lang.Class.getClassLoader() 方法的声明。
public ClassLoader getClassLoader()
参数
NA
返回值
该方法返回加载了该对象所代表的类或接口的类加载器。
异常
SecurityException − 如果安全管理器存在并且其 checkPermission 方法拒绝访问该类的类加载器。
示例
下面的例子展示了 java.lang.Class.getClassLoader() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class ClassDemo {
public static void main(String[] args) {
try {
// returns the Class object associated with this class
Class cls = Class.forName("com.tutorialspoint.ClassDemo");
// returns the ClassLoader object associated with this Class.
ClassLoader cLoader = cls.getClassLoader();
if (cLoader == null) {
System.out.println("The default system class was used.");
} else {
// returns the class loader
Class loaderClass = cLoader.getClass();
System.out.println("Class associated with ClassLoader = " +
loaderClass.getName());
}
} catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Class associated with ClassLoader = sun.misc.Launcher$AppClassLoader

