Close

Java Reflection - Class.getEnumConstants() 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 T[] getEnumConstants()
Returns:
an array containing the values comprising the enum class represented by this Class object in the order they're declared, or null if this Class object does not represent an enum type

Examples


package com.logicbig.example.clazz;

import java.util.Arrays;

public class GetEnumConstantsExample {

public static void main(String... args) {
Class<MyEnum> c = MyEnum.class;
MyEnum[] myEnums = c.getEnumConstants();
System.out.println(Arrays.toString(myEnums));
Arrays.stream(myEnums).forEach(MyEnum::doSomething);
}

private enum MyEnum {
A, B, C;

private void doSomething() {
System.out.println("doing something " + this.name());
}
}
}

Output

[A, B, C]
doing something A
doing something B
doing something C




See Also