I have an interface:
public interface NamedEnum {
String getName();
}
An enum which implements the interface:
public enum MyEnum implements NamedEnum {
MYVALUE1("My value one"),
MYVALUE2("My value two");
private String name;
private MyEnum(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
A method which doesn't compile:
public static Map<Integer,String> wrong(Enum<? extends NamedEnum> value) {
Map<Integer,String> result = new LinkedHashMap<Integer, String>();
for (Enum<? extends NamedEnum> val : value.values())
result.put(val.ordinal(), val.getName());
return result;
}
Two errors:
The method values() is undefined for the type Enum<capture#1-of ? extends NamedEnum>
The method getName() is undefined for the type Enum<capture#3-of ? extends NamedEnum>
I can't figure out how the method above can accept an enum which implements the interface.
ordinal. Quoting the Javadoc: "Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap." You've not said what you're doing, but take heed. Also, check out Effective Java 2nd ed Item 33: "Use EnumMap instead of ordinal indexing".ordinalquote in the javaDoc but I was lazy and didn't check theEnumSet/Map: my bad. I need my enum to build select/options like this<option value="0">My value one</option>and so on... I understand I can do it simply like this instead<option value="MYVALUE1">My value one</option>and get the values withvalueOf("MYVALUE1").