Home »
Java programming language
Java Class class getClassLoader() method with example
Class class getClassLoader() method: Here, we are going to learn about the getClassLoader() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 23, 2019
Class class getClassLoader() method
- getClassLoader() method is available in java.lang package.
- getClassLoader() method is used to return the ClassLoader that loads the class or interface.
- getClassLoader() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- getClassLoader() method may throw SecurityException at the time of loading class or interface.
SecurityException: In this exception, its checkPermission() method does not allow access classloader for the class when the security manager exists.
Syntax:
public ClassLoader getClassLoader();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is ClassLoader, it returns the following values based on the given cases,
- It returns class loader that loads the class denoted by this object.
- It returns null in case of bootstrap class loader because we don't need to implement bootstrap class loader.
Example:
// Java program to demonstrate the example
// of ClassLoader getClassLoader() method of Class
public class GetClassLoaderOfClass {
public static void main(String[] args) throws Exception {
// It returns the Class object attached with the given
//classname
Class cl = Class.forName("GetClassLoaderOfClass");
// By using getClassLoader() is to load the class
ClassLoader class_load = cl.getClassLoader();
// If any ClassLoader associate with the Class
if (class_load != null) {
Class load_class = class_load.getClass();
System.out.print("Associated Loader Class: ");
System.out.print(load_class.getName());
}
// No Loader associated with the class
else
System.out.println("No system loader associated with the class");
}
}
Output
Associated Loader Class: jdk.internal.loader.ClassLoaders$AppClassLoader