Home » Java programming language

Java ClassLoader findLoadedClass() method with example

ClassLoader Class findLoadedClass() method: Here, we are going to learn about the findLoadedClass() method of ClassLoader Class with its syntax and example.
Submitted by Preeti Jain, on November 27, 2019

ClassLoader Class findLoadedClass() method

  • findLoadedClass() method is available in java.lang package.
  • findLoadedClass() method is used to return the Class with the given binary class name when this loader has been recorded by JVM as initializing the loader of the class with that binary name.
  • findLoadedClass() 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.
  • findLoadedClass() method may throw an exception at the time of loading Class object.

Syntax:

    protected Class findLoadedClass(String class_name);

Parameter(s):

  • String class_name – represents the binary name of the class.

Return value:

The return type of this method is Class, it returns Class object when the class has been recorded otherwise it returns null, when the class has not been recorded.

Example:

// Java program to demonstrate the example 
// of Class findLoadedClass(String class_name)
// method of ClassLoader 

class FindLoadedClass extends ClassLoader {
    void loadedClass() {
        // It checks whether the given class is loaded
        // or not by using the findLoadedClass()
        Class cl1 = super.findLoadedClass("java.lang.String");

        // If cl1 not null that means cl1 is loaded
        // then don't need to load again
        if (cl1 != null)
            System.out.println(" Class already loaded!!!");
        else
            System.out.println("Ready to load the given class by using loadClass()!!!");
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        // Creating an instance of FindLoadedClass
        FindLoadedClass lc = new FindLoadedClass();
        lc.loadedClass();
    }
}

Output

Ready to load the given class by using loadClass()!!!


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.