I am trying to initialise a generic class with all the availables values from an Enum. HEre is how I would like it to work:
public class MyClass<E extends Enum<E>> {
E[] choices;
public MyClass() {
choices = E.values();
}
However, the call to E.values
is not accepted in Eclipse saying that this method is undefined for this E.
Using this constructor instead is accepted, but requires the caller to provide the values:
public MyClass(E[] e) {
choices = e;
}
In the documentation I found:
Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.
Is there a way to workaround this issue ?
No, we cannot extend an enum in Java. Java enums can extend java. lang. Enum class implicitly, so enum types cannot extend another class.
Enum and has several static members. Therefore enum cannot extend any other class or enum: there is no multiple inheritance. Class cannot extend enum as well. This limitation is enforced by compiler.
However, it is possible to use enums in generics. The MSDN article for Enum gives the following type definition for the class Enum . This definition can be used to get enum s working as generic types by constraining the generic type to those of Enum .
Enums cannot inherit from other enums. In fact all enums must actually inherit from System. Enum . C# allows syntax to change the underlying representation of the enum values which looks like inheritance, but in actuality they still inherit from System.
Your best option is probably to pass a Class
of the desired enum to the constructor. (See how an EnumMap
is constructed for instance.)
You can then use clazz.
getEnumConstants
()
to get hold of the constants.
Your first code snippet doesn't work because of how Generics is implemented in Java, and due to a phenomenon known as type erasure. Because of this, the JVM will have no idea what the type of E
is when your program is executing.
You could probably do something like this in MyClass
's constructor:
public MyClass(E type) {
choices = type.getDeclaringClass().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