Java Class getClasses() Method



Description

The Java Class getClasses() method returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object. This includes public class and interface members inherited from superclasses and public class and interface members declared by the class.

Declaration

Following is the declaration for Java Class getClasses() method

public Class<?>[] getClasses()

Parameters

NA

Return Value

This method returns the array of Class objects representing the public members of this class.

Exception

SecurityException − If a security manager, s, is present.

Getting Member Classes and Interfaces of a Class Example

The following example shows the usage of java.lang.Class.getClasses() method. In this program, we've retrieved the Class of ClassDemo and then using getClasses(), we've retrieved the all the member classes and then name of each class is printed.

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         // returns the Class object associated with this class
         Class cls = Class.forName("com.tutorialspoint.ClassDemo");

         /* returns the array of Class objects representing the public 
            members of this class */
         Class[] classes = cls.getClasses();
         for (int i = 0; i < classes.length; i++) {
            System.out.println("Class found = " + classes[i].getName());
         }
      } catch (ClassNotFoundException e) {
         System.out.println(e.toString());
      }
   }

   public class InnerClass1 {
      public InnerClass1() {
         System.out.println("Inner Class1");
      }
   }

   public class InnerClass2 {
      public InnerClass2() {
         System.out.println("Inner Class2");
      }
   }

   private class InnerPrivateClass {
      public InnerPrivateClass() {
         System.out.println("Inner Private Class");
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Class found = com.tutorialspoint.ClassDemo$InnerClass1
Class found = com.tutorialspoint.ClassDemo$InnerClass2

Getting Member Classes and Interfaces of Thread Class Example

The following example shows the usage of java.lang.Class.getClasses() method. In this program, we've retrieved the Class of Thread Class and then using getClasses(), we've retrieved the all the member classes and then name of each class is printed.

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String[] args) {
      /* returns the array of Class objects representing the public 
         members of this class */
      Class[] classes = Thread.class.getClasses();
      for (int i = 0; i < classes.length; i++) {
         System.out.println("Class found = " + classes[i].getName());
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Class found = java.lang.Thread$UncaughtExceptionHandler
Class found = java.lang.Thread$State
Class found = java.lang.Thread$Builder

Getting Member Classes and Interfaces of ArrayList Class Example

The following example shows the usage of java.lang.Class.getClasses() method. In this program, we've retrieved the Class of ArrayList Class and then using getClasses(), we've retrieved the all the member classes and then length of array is printed being zero.

package com.tutorialspoint;

import java.util.ArrayList;

public class ClassDemo {

   public static void main(String[] args) {

      /* returns the array of Class objects representing the public 
         members of this class */
      Class[] classes = ArrayList.class.getClasses();
      System.out.println("Classes found = " + classes.length);
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Classes found = 0
java_lang_class.htm
Advertisements