Home » Java programming language

Java Class class getClasses() method with example

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

Class class getClasses() method

  • getClasses() method is available in java.lang package.
  • getClasses() method is used to return an array that contains Class objects denoting all the public classes and interfaces that are a member of the class denoted by this Class object and it includes public class and interface members inherited from parent classes.
  • getClasses() 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.
  • getClasses() method may throw SecurityException at the time of checking security constraints.SecurityException: This exception raise when security manager exists.

Syntax:

    public Class[] getClasses();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Class[], it returns an array of Class objects denoting the public members of this class.

Example:

// Java program to demonstrate the example 
// of Class[] getClasses() method of  Class 

public class GetClassesOfClass {
    public static void main(String[] args) throws Exception {
        // It returns the Class object attached 
        // with the given classname
        Class cl = Class.forName("java.lang.ClassLoader");

        // It returns an array of Class objects denotes the public
        // member of the class "java.lang.ClassLoader"
        Class[] public_classes = cl.getClasses();

        // Traversing ClassLoader class
        for (int i = 1; i <= public_classes.length; ++i) {
            System.out.print("Class Name: ");
            System.out.println(public_classes[i].getName());
        }
    }
}

Output






Comments and Discussions!

Load comments ↻






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