I have issues storing an enum value and then querying it via the toString()
method. I require to handcode the numbers. How do I safely override my toString()
?
public enum Result {
SUCCESS(0),
INVALID_UPLOAD_KEY(-20),
UPLOAD_KEY_NOT_FOUND(-80);
private final int value; //do I need this?
private Result(int value) {
this.value = value;
}
public static Result fromInt(final int n) {
for (final Result result : values()) {
if (result.getValue() == n) {
return result;
}
}
return null;
}
public int getValue() {
return this.value;
}
public String toString() {
switch (this.value) {
case UPLOAD_KEY_NOT_FOUND: //Type mismatch: cannot convert from MyClass.Result to int
return "Upload Key not found";
case INVALID_UPLOAD_KEY: //Type mismatch: cannot convert from MyClass.Result to int
return "Invalid Upload Key";
case SUCCESS: //Type mismatch: cannot convert from MyClass.Result to int
return "Success";
default:
return "No result code associated with: " + this.value;
}
}
}
Eclipse complains the toString
method
Type mismatch: cannot convert from MyClass.Result to int