Java.lang.Class.getClasses() Method



Description

The java.lang.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.lang.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.

Example

The following example shows the usage of java.lang.Class.getClasses() method.

package com.tutorialspoint;

import java.lang.*;

public class ClassDemo {

   public static void main(String[] args) {

      try {
         // returns the Class object associated with this class
         Class cls = Class.forName("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");
      }
   }
} 

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

java.lang.ClassNotFoundException: ClassDemo
java_lang_class.htm
Advertisements