Home » Java programming language

Java ClassLoader findSystemClass() method with example

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

ClassLoader Class findSystemClass() method

  • findSystemClass() method is available in java.lang package.
  • findSystemClass() method is used to find the class with the given binary name and load the class through the system loader if needed.
  • findSystemClass() 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.
  • findSystemClass() method may throw an exception at the time of finding the class with the given class.
    ClassNotFoundException: This exception may throw when the given class does not exist.

Syntax:

    protected Class findSystemClass(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 a Class object for the given class name.

Example:

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

class FindSystemClass extends ClassLoader {
    void loadedSystemClass() throws ClassNotFoundException {
        // It checks whether the given class is loaded
        // or not by using the findSystemClass()
        Class cl1 = super.findSystemClass("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 system classloader!!!");
    }
}

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

Output

Class already loaded!!!


Comments and Discussions!

Load comments ↻





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