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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.