Once, I had this particular case where I needed to have an enumeration with the double java keyword. I also wanted that a call on this enumeration would return me the “double” String.
If you try to type this :
1 2 3 4 5 |
public enum Style { dotted, dashed, solid, double, groove, ridge, inset, outset; } |
You will get an error from the compiler saying that you cannot use the double keyword.
Here an elegant solution to achieve this :
1 2 3 4 5 6 7 8 9 10 11 |
public enum Style { DOTTED, DASHED, SOLID, DOUBLE, GROOVE, RIDGE, INSET, OUTSET; @Override public String toString() { return this.name().toLowerCase(); } } |
This is possible due to the fact the enum is a class. We can then manipulate the name attribute
Leave a Reply