Correspondig the following question:
Java: Enum parameter in method
I would like to know, how can I format the code to require enums generically.
Foo.java
public enum Foo {
a(1), b(2);
}
Bar.java
public class Bar {
public Bar(generic enum);
}
Later on I’ll have more enum classes like “foo”, but you can still create bar containing any kind of enum class. I have “jdk1.6.0_20” by the way…
See the methods in EnumSet for reference, e.g.
(This method returns an EnumSet with one element from a given Enum element e)
So the generic bounds you need are:
<E extends Enum<E>>Actually, you will probably make
Baritself generic:You may also add a factory method like
from,withetc.That way, in client code you only have to write the generic signature once:
Reference: