Skip to content
Advertisement

Print Java enum to lower case by default keeping enum constants in uppercase

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:

JavaScript

I’d like to get the following:

JavaScript

[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:

JavaScript

This would output the name in lower case.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement