Generics using Enum in Java
enums are final types, which means, you can not extend from them
a generic like wants a Class as Parameter which is Days or an extended class, but the extended class is not possible
so the only parameter possible is Days and you don't need a generic, if only one value is possible
You cannot extend enums in java (http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html towards the end). Thus is not possible.
-IF you need your class to work only with your enum you don't need generics
-if instead you need it to work with others enum (does it makes sense?) you don't need to specify the extend.
What does exactly your State class do ?
Don't use a generics bound. There's no need. Just use an unbounded type, like this:
public class State<T> {
public State(T startState) {
// whatever
}
}
And to use it:
State<Days> dayState = new State<Days>(Days.SUNDAY);
This is a straightforward typed class that doesn't need a bound.
The only bound that might make sense is a bound to an enum
:
public class State<T extends Enum<T>> {
public State(T startState) {
// whatever
}
}
This version requires that the generic parameter be an enum
. With this version the above usage example would still compile, but this would not compile (for example):
State<String> dayState = new State<String>("hello");
because String
is not an enum
.