What is the best way to use the values stored in an Enum as String literals?
For example:
public enum Modes {
some-really-long-string,
mode1,
mode2,
mode3
}
Then later I could use Mode.mode1
to return its string representation as mode1
. Without having to keep calling Mode.mode1.toString()
.
Best Answer
You can't. I think you have FOUR options here. All four offer a solution but with a slightly different approach...
Option One: use the built-in
name()
on an enum. This is perfectly fine if you don't need any special naming format.Option Two: add overriding properties to your enums if you want more control
Option Three: use static finals instead of enums:
Option Four: interfaces have every field public, static and final: