I have a long list of enumerations that are being referenced and selected via if-statements in my code, however, I want to use switch statements as I have a huge number of such statements in multiple places. This is a small subset of the enumerations that I have:
public enum MYENUM {
FOO1("hello"),
FOO2("goodbye"),
private final String toString;
MYENUM(String toString) {
this.toString = toString;
}
@Override
public String toString() {
return this.toString;
}
}
Previously I had:
String toCheck = "hello;"
if (toCheck.equals(MYENUM.FOO1.toString())) {
//Do stuff
}
if (toCheck.equals(MYENUM.FOO2.toString())) {
//Do stuff
}
But now I want:
String toCheck = "hello;"
switch(toCheck) {
case MYENUM.FOO1.toString():
//Do stuff
break;
case MYENUM.FOO2.toString():
//Do stuff
break;
}
Needless to say, this does not work, I can't even enter the toString() function of the Enum in the case, 'toString' has private access in 'MYENUM'
. How can I modify my switch statement so that I can properly switch on the Strings?