Reputation: 7856
Here's my code .Basically I trying to return a Map from my intToEnumMap
method.But compiler is not allowing this.Why can't the compiler infer the return type for my method?
public enum INVALIDTHRESHOLDSTATE{
ONE(0,GAUGE_MIN_LOWER_THRESHOLD1_MESSAGE),
TWO(1,GAUGE_THRESHOLD1_LOWER_THRESHOLD2_MESSAGE),
THREE(2,GAUGE_THRESHOLD2_LOWER_MAX_MESSAGE);
private static final Map<Integer,INVALIDTHRESHOLDSTATE> intToEnum = new HashMap<Integer, INVALIDTHRESHOLDSTATE>();
static {
int i=0;
for (INVALIDTHRESHOLDSTATE invalidState : values()){
i++;
intToEnum.put(Integer.valueOf(i), invalidState);
}
}
private int value;
private String message;
private INVALIDTHRESHOLDSTATE(int value,String message){
this.value = value;
this.message = message;
}
public int getValue() {
return value;
}
public String getString() {
return message;
}
// Generic method
public static <K,V> Map<K,V> intToEnumMap() {
return intToEnum; //doesn't compile
^^^^^^^^^
}
}
Upvotes: 3
Views: 109
Reputation: 1504172
You've created a generic method, so anyone could call:
Map<String, Long> map = INVALIDTHRESHOLDSTATE.<String, Long> intToEnumMap();
I assume that's not what you intended - if you're returning intToEnum
it looks to me like you don't want the callers to specify generic type arguments at all, and it should just be:
public static Map<Integer, INVALIDTHRESHOLDSTATE> intToEnumMap() {
return intToEnum;
}
(I'd also strongly suggest renaming your enum to follow normal Java naming conventions, but that's a different matter.)
Upvotes: 8