24

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
spacebiker
  • 3,777
  • 4
  • 30
  • 49

4 Answers4

62

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.

a short note for all those unbelievers: You can find usage like this in Google applications (ie: play.google.com/store/apps/…) , where they overwrite the toString function of some objects to get the desired string. I repeat: it's just a matter of the developer preference, and as long as it is well documented for me it's a valid solution.

spacebiker
  • 3,777
  • 4
  • 30
  • 49
  • 20
    This is not a very good solution (despite the upvotes) because now `toString()` and `name()` no longer return the same value. True, it doesn't say anywhere that they should but it's their default behavior that they do. Furthermore by overriding `toString()` you only catch code use such as in your example - it's kind of an implicit contract. That's not clean design. Since you cannot override `name()` I'd much rather provide a new method like `lowerCaseName()`. – Marcel Stör Nov 10 '13 at 19:47
  • 9
    It is absolutely a clean way to do it, otherwise toString would not be overridable, it works as expected for me. I do not want to use a new method lowerCase as in code, i want to get the lowercase value directly. Just a matter of preference from the developer. – spacebiker Nov 10 '13 at 19:48
  • 5
    toString is overridable on all objects, that doesn't make your usage of it appropriate. I have to agree with Marcel, and in fact since you appear to be wanting a readable label from the enum, you should just put in a getLabel() method. Then you have a well defined method with a well defined return value. toString() is neither of these things, and is only used in this fashion in a few extremely limited cases where an objects value has a defacto standard format, like Integer, Boolean and String. Other than these few fringe cases, you should never rely on the output of toString(). – Robin Jun 09 '14 at 14:27
  • You can find usage like this in Google applications (ie: https://play.google.com/store/apps/details?id=com.google.android.apps.iosched) , where they overwrite the toString function of some objects to get the desired string. I repeat: it's just a matter of the developer preference, and as long as it is well documented for me it's a valid solution. – spacebiker Jun 12 '14 at 13:17
  • 13
    As made clear in this post, name() is really there to return the original name of the declared enum, which is marked final, and developers know it wont be overridden, while toString() is intended (and even encouraged) to be used to return a "friendly" name, so this usage seems in line with the recommendations - http://stackoverflow.com/questions/18031125/what-is-the-difference-between-enum-name-and-enum-tostring – chrismarx Jun 18 '15 at 17:04
16

Another solution could be:

public enum Status {
  DRAFT, PENDING, COMPLETE;

  public String nameLowerCase(){
        return name().toLowerCase();
    }
}
Lorenzo Sciuto
  • 1,655
  • 3
  • 27
  • 57
7

If you want lower case, you could just use lower case, or mixed case, or whatever makes more sense to you.

public enum Status {
    draft, pending, complete;
}

println ("Status=" + Status.draft);

prints

Status=draft
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Hi Peter, thanks for your reply, but look for mine below, i solved it by overriding the Enum.toString function. Your answer does not solve my question as i'd like to use the constant enum in uppercase, but when requesting the real value get the lowercase representation. – spacebiker Nov 10 '13 at 19:39
  • 1
    IMHO either upper case or lower case is more readable and make more sense to put in your output or code. It does answer the question because you didn't say you need to have your constants in upper case. – Peter Lawrey Nov 10 '13 at 19:48
  • 1
    You are right, i did not specify i wanted constants in uppercase, i thought it was obvious. I edited the question.Thanks – spacebiker Nov 10 '13 at 19:51
  • Seeing an uppercase identifier lets you know you are dealing with a constant value. Seeing a lower case enum means you cannot trust that naming conventions are adhered to in this codebase... "All bets are off". This will also fail static analysis linting (like Sonar) so once you make this exception, it's turtles all the way down. – Colm Oct 17 '22 at 10:22
-3

You can use the following Enum class which contains constructor with name and ordinal for each enum constant. You can assign values you need for the enum constant.

public enum Status {

DRAFT(0,"draft"), PENDING(1,"pending"), COMPLETE(2,"complete");

private int key;
private String value;

Status(int key, String value){
    this.key = key;
    this.value = value;
}

public int getKey() {
    return key;
}

public void setKey(int key) {
    this.key = key;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

@Override
public String toString(){
    return this.value;
}

}

Since we override the toString method, the value which is lowercase is returned.

Using

System.out.print("Status = "+Status.DRAFT);

would print,

Status = draft

and

System.out.print("Status = "+Status.DRAFT.name());

would print

Status = DRAFT
Lucky
  • 16,787
  • 19
  • 117
  • 151
  • There's no need to store the `value` as a separate string since you can just call `name().toLowerCase()` . – aarbor Feb 12 '18 at 23:10