Java 类 getClassLoader() 方法



描述

Java 类 getClassLoader() 方法返回类的类加载器。某些实现可能使用 null 来表示引导类加载器。如果此类由引导类加载器加载,则此方法在这些实现中将返回 null。

声明

以下是 java.lang.Class.getClassLoader() 方法的声明

public ClassLoader getClassLoader()

参数

返回值

此方法返回加载此对象所表示的类或接口的类加载器。

异常

SecurityException - 如果存在安全管理器并且其 checkPermission 方法拒绝访问类的类加载器。

示例

以下示例显示了 java.lang.Class.getClassLoader() 方法的使用。

获取类的类加载器示例

以下示例显示了 java.lang.Class.getClassLoader() 方法的使用。在此程序中,我们已检索 ClassDemo 的 Class,然后使用 getClassLoader() 检索类加载器,然后打印其名称。

package com.tutorialspoint;

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

获取 List 类的类加载器示例

以下示例显示了 java.lang.Class.getClassLoader() 方法的使用。在此程序中,我们已检索 List 类的 Class,然后使用 getClassLoader() 检索类加载器,然后打印其名称。

package com.tutorialspoint;

import java.util.List;

public class ClassDemo {

   public static void main(String[] args) {
      // returns the Class object associated with List class
      Class cls = List.class;

      // 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());
      }
   }
}  

输出

让我们编译并运行以上程序,这将产生以下结果:

The default system class was used.

获取 Integer 类的类加载器示例

以下示例显示了 java.lang.Class.getClassLoader() 方法的使用。在此程序中,我们已检索 Integer 类的 Class,然后使用 getClassLoader() 检索类加载器,然后打印其名称。

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String[] args) {
      // returns the Class object associated with List class
      Class cls = Integer.class;

      // 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());
      }
   }
}  

输出

让我们编译并运行以上程序,这将产生以下结果:

The default system class was used.
java_lang_class.htm
广告

© . All rights reserved.