Close

Java Reflection - Class.getClasses() Examples

Code Snippets Java Reflection Java Java API 


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<?>[] getClasses()

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. This method returns an array of length 0 if this Class object has no public member classes or interfaces. This method also returns an array of length 0 if this Class object represents a primitive type, an array class, or void.

Returns:
the array of Class objects representing the public members of this class


Examples


package com.logicbig.example.clazz;

import java.io.Serializable;
import java.util.Arrays;

public class GetClassesExample {

public static void main(String... args) {
Class<?>[] classes = GetClassesExample.class.getClasses();
Arrays.stream(classes)
.forEach(System.out::println);
}

public class MyClass extends MyClass2 implements Cloneable {
}

public class MyClass2 implements Serializable {

}

public interface MyInterface extends Serializable {
}

public static class MyStaticClass {
}

public static class MyMainSuperClass {

}
}

Output

class com.logicbig.example.clazz.GetClassesExample$MyMainSuperClass
class com.logicbig.example.clazz.GetClassesExample$MyStaticClass
interface com.logicbig.example.clazz.GetClassesExample$MyInterface
class com.logicbig.example.clazz.GetClassesExample$MyClass2
class com.logicbig.example.clazz.GetClassesExample$MyClass




package com.logicbig.example.clazz;

import java.util.Arrays;

public class GetClassesExample2 {

public static void main(String... args) {
String[] strings = {};
Class<?>[] classes = strings.getClass().getClasses();
System.out.println(Arrays.toString(classes));
}
}

Output

[]




If Member classes are not public:

package com.logicbig.example.clazz;

import java.util.Arrays;

public class GetClassesExample3 {

public static void main(String... args) {
Class<?>[] classes = GetClassesExample3.class.getClasses();
System.out.println(Arrays.toString(classes));
}

class A {
}

interface B {
}

private class C {
}

protected interface D {
}
}

Output

[]




package com.logicbig.example;

public class MainSuperClass {
public class MainSuperNestedClass {
}

public interface MainSuperNestedInterface {
}
}

package com.logicbig.example;

public class MainClass extends MainSuperClass {
public static void main(String[] args) {
for (Class<?> aClass : MainClass.class.getClasses()) {
System.out.println(aClass);
}
}
}

Output

interface com.logicbig.example.MainSuperClass$MainSuperNestedInterface
class com.logicbig.example.MainSuperClass$MainSuperNestedClass

package com.logicbig.example;

public interface MainInterface {
public class MainInterfaceNestedClass {
}

public interface MainInterfaceNestedInterface {
}
}

package com.logicbig.example;

public class MainClass implements MainInterface {
public static void main(String[] args) {
Class<?>[] classes = MainClass.class.getClasses();
System.out.println(classes.length);
for (Class<?> aClass : classes) {
System.out.println(aClass);
}
}
}

Output

0




See Also