Java.lang Package Classes

Java Class - getClassLoader() Method



The java.lang.Class.getClassLoader() method 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.

If this object represents a primitive type or void, null is returned.

Syntax

public ClassLoader getClassLoader()

Parameters

No parameter is required.

Return Value

Returns the class loader that loaded the class or interface represented by this object.

Exception

Throws SecurityException, if a security manager exists and its checkPermission method denies access to the class loader for the class.

Example:

In the example below, the java.lang.Class.getClassLoader() method is used to get the class loader for the given class.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    MyClass x = new MyClass();
    Class cls = x.getClass();

    //print the CanonicalName of the class
    ClassLoader clsLoader = cls.getClassLoader();
    System.out.println("ClassLoader = " + clsLoader);
  }
}

The output of the above code will be:

ClassLoader = jdk.internal.loader.ClassLoaders$AppClassLoader@8bcc55f

❮ Java.lang - Class

5