Java enums vs constants for Strings
- by Marcus
I've switched from using constants for Strings:
public static final String OPTION_1 = "OPTION_1";
... to enums:
public enum Options {
OPTION_1;
}
With constants, you'd just refer to the constant:
String s = TheClass.OPTION_1
But with Enums, you have to specify toString():
String s = Options.OPTION_1.toString();
I don't like that you have to use the toString() statement, and also, in some cases you can forget to include it which can lead to unintended results.. ie:
Object o = map.get(Options.OPTION_1); //This won't work as intended if the Map key is a String
Is there a better way to use enums for String constants?