![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java Class getDeclaredClasses() Method
Description
The Java Class getDeclaredClasses() method returns an array of Class objects including public, protected, default (package) access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces.
This method returns an array of length 0 if the class declares no classes or interfaces as members, or if this Class object represents a primitive type, an array class, or void.
Declaration
Following is the declaration for java.lang.Class.getDeclaredClasses() method
public Class<?>[] getDeclaredClasses() throws SecurityException
Parameters
NA
Return Value
This method returns the array of Class objects representing all the declared members of this class.
Exception
SecurityException − If a security manager, s, is present.
Getting All the Declared Classes Example
The following example shows the usage of java.lang.Class.getDeclaredClasses() method. In this program, the class of the ClassDemo is retrieved using forName() method. Using getDeclaredClasses(), we've retrieved inner classes declared inside ClassDemo class.
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("ClassDemo"); /* returns the array of Class objects representing all the declared members of this class */ Class[] classes = cls.getDeclaredClasses(); for (int i = 0; i < classes.length; i++) { System.out.println("Class = " + 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 = ClassDemo$InnerPrivateClass Class = ClassDemo$InnerClass2 Class = ClassDemo$InnerClass1
Getting All the Declared Classes of ArrayList Example
The following example shows the usage of java.lang.Class.getDeclaredClasses() method. In this program, the class of the ArrayList is retrieved using class property. Using getDeclaredClasses(), we've retrieved inner classes declared inside ClassDemo class.
package com.tutorialspoint; import java.util.ArrayList; public class ClassDemo { public static void main(String[] args) { // returns the Class object associated with this class Class cls = ArrayList.class; /* returns the array of Class objects representing all the declared members of this class */ Class[] classes = cls.getDeclaredClasses(); for (int i = 0; i < classes.length; i++) { System.out.println("Class = " + classes[i].getName()); } } }
Output
Let us compile and run the above program, this will produce the following result −
Class = java.util.ArrayList$ListItr Class = java.util.ArrayList$Itr Class = java.util.ArrayList$SubList Class = java.util.ArrayList$ArrayListSpliterator
Getting All the Declared Classes of Thread Example
The following example shows the usage of java.lang.Class.getDeclaredClasses() method. In this program, the class of the Thread is retrieved using class property. Using getDeclaredClasses(), we've retrieved inner classes declared inside ClassDemo class.
package com.tutorialspoint; public class ClassDemo { public static void main(String[] args) { // returns the Class object associated with this class Class cls = Thread.class; /* returns the array of Class objects representing all the declared members of this class */ Class[] classes = cls.getDeclaredClasses(); for (int i = 0; i < classes.length; i++) { System.out.println("Class = " + classes[i].getName()); } } }
Output
Let us compile and run the above program, this will produce the following result −
Class = java.lang.Thread$FieldHolder Class = java.lang.Thread$ThreadIdentifiers Class = java.lang.Thread$Constants Class = java.lang.Thread$ThreadNumbering Class = java.lang.Thread$UncaughtExceptionHandler Class = java.lang.Thread$State Class = java.lang.Thread$Caches Class = java.lang.Thread$Builder