Java Reflection - Class.getDeclaredClasses() Examples

Code Snippets Java Reflection Java 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public Class<?>[] getDeclaredClasses()
                              throws SecurityException
Returns:
the array of Class objects representing all the declared members of this class


Examples


package com.logicbig.example.clazz;

import java.util.Arrays;

public class GetDeclaredClassesExample {

public static void main(String... args) {
System.out.println("-- declared classes --");
Class<?>[] classes = MyClass.class.getDeclaredClasses();
Arrays.stream(classes)
.forEach(c -> System.out.println(c.getSimpleName()));

System.out.println("-- classes --");

classes = MyClass.class.getClasses();
Arrays.stream(classes)
.forEach(c -> System.out.println(c.getSimpleName()));

}

public static class MyClass extends SuperClass {
private static interface APrivateInterface {
}

private static class APrivateClass {
}

public static class APublicClass {
}
}

private static class SuperClass {
public static interface ASuperPublicInnerInterface {
}

public static interface ASuperPublicInnerClass {
}
}
}

Output

-- declared classes --
APublicClass
APrivateClass
APrivateInterface
-- classes --
APublicClass
ASuperPublicInnerClass
ASuperPublicInnerInterface




See Also