How can I get the values of an "enum" in a generic?
Class::getEnumConstants
You cannot directly get it from T
because generics are erased by the Java compiler so at runtime it is no longer known what T
is.
What you can do is require a Class<T>
object as constructor parameter. From there you can get an array of the enum objects by calling Class::getEnumConstants
.
public class Sorter<T extends Enum<T>> {
public Sorter(Class<T> clazz) {
final T[] enumConstants = clazz.getEnumConstants();
}
}