I've got the class object for an enum (I have a Class<? extends Enum>
) and I need to get a list of the enumerated values represented by this enum. The values
static function has what I need, but I'm not sure how to get access to it from the class object.
No, we cannot extend an enum in Java. Java enums can extend java. lang. Enum class implicitly, so enum types cannot extend another class.
We cannot extend enum classes in Java. It is because all enums in Java are inherited from java. lang. Enum .
You cannot have an enum extend another enum , and you cannot "add" values to an existing enum through inheritance.
Which class does all the Enums extend? Explanation: All enums implicitly extend java. lang. Enum.
Class.getEnumConstants
If you know the name of the value you need:
Class<? extends Enum> klass = ... Enum<?> x = Enum.valueOf(klass, "NAME");
If you don't, you can get an array of them by (as Tom got to first):
klass.getEnumConstants();
using reflection is simple as calling Class#getEnumConstants():
List<Enum<?>> enum2list(Class<? extends Enum<?>> cls) {
return Arrays.asList(cls.getEnumConstants());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With