Java.lang Package Classes

Java ClassLoader - getParent() Method



The java.lang.ClassLoader.getParent() method returns the parent class loader for delegation. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class loader's parent is the bootstrap class loader.

Syntax

public final ClassLoader getParent()

Parameters

No parameter is required.

Return Value

Returns the parent ClassLoader.

Exception

Throws SecurityException, If a security manager exists and its checkPermission method doesn't allow access to this class loader's parent class loader.

Example:

In the example below, the java.lang.ClassLoader.getParent() method is used to get parent class loader for delegation.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) throws ClassNotFoundException {
      //loads the class
      Class cls = Class.forName("MyClass");

      //returns the ClassLoader object associated with this Class
      ClassLoader cL = cls.getClassLoader();
    
      //print ClassLoader
      System.out.println("ClassLoader: ");
      System.out.println(cL.getClass());
    
      //print parent ClassLoader
      System.out.println("parent ClassLoader: ");
      System.out.println(cL.getParent()); 
  }
}

The output of the above code will be:

ClassLoader: 
class jdk.internal.loader.ClassLoaders$AppClassLoader
parent ClassLoader: 
jdk.internal.loader.ClassLoaders$PlatformClassLoader@7960847b

❮ Java.lang - ClassLoader