I have an enum in Java I’d like to serialize, so that when I call it from anywhere in the code, I get the lowercase representation of the name.
Let’s say I have the following enum:
public enum Status { DRAFT, PENDING, COMPLETE; } println ("Status=" + Status.DRAFT);
I’d like to get the following:
Status=draft
[Note]: I want to use the enum constants in uppercase, and when requesting the value get the lowercase representation.
Advertisement
Answer
I am replying this question myself as i found the solution interesting and could not find a reply in the site. Just in case somebody else looks for a way to solve this.
The solution is simple, just override the Enum toString method like this:
public enum Status { DRAFT, PENDING, COMPLETE; @Override public String toString() { return name().toLowerCase(); } } println ("Status=" + Status.DRAFT);
This would output the name in lower case.