97

I have a enum defined like this and I would like to be able to obtain the strings for the individual statuses. How should I write such a method?

I can get the int values of the statuses but would like the option of getting the string values from the ints as well.

public enum Status {
    PAUSE(0),
    START(1),
    STOP(2);

    private final int value;

    private Status(int value) {
        this.value = value
    }

    public int getValue() {
        return value;
    }
}
7
  • 7
    Status s = Status.PAUSE; System.out.println(s.name()); Commented Jul 19, 2013 at 8:47
  • This answer will help you understand stackoverflow.com/questions/17606866/… Commented Jul 19, 2013 at 8:50
  • 2
    Instead of defining 0,1,2, you can use Status.ordinal(); Commented Jul 19, 2013 at 8:59
  • 2
    @ArnaudDenoyelle: You should avoid using ordinal(), see Joshua Bloch "Effective Java" Commented Aug 3, 2015 at 12:38
  • 2
    @GarfieldKlon I agree. I don't (and will never) advise to rely on ordinal() because adding a value in the enum will shift every enum's ordinal. I wanted to point out that the OP duplicated the ordinal()'s behavior. Commented Aug 3, 2015 at 12:46

6 Answers 6

132

if status is of type Status enum, status.name() will give you its defined name.

Sign up to request clarification or add additional context in comments.

2 Comments

@JoeMaher toString() seems to be the preferred way as per the official documentation: "Most programmers should use the toString() method in preference to this one, as the toString method may return a more user-friendly name." (docs.oracle.com/javase/6/docs/api/java/lang/Enum.html#name())
@Carsten status.name() is appropriate if you want to use it in the code (for accuracy and consistency), status.toString() is appropriate if you want to show it to the user (for readability).
50

You can use values() method:

For instance Status.values()[0] will return PAUSE in your case, if you print it, toString() will be called and "PAUSE" will be printed.

1 Comment

Right, just to add Status.values()[0] would be of type Status not String.
11

Use default method name() as given bellows

public enum Category {
        ONE("one"),
        TWO ("two"),
        THREE("three");

        private final String name;

        Category(String s) {
            name = s;
        }

    }

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Category.ONE.name());
    }
}

3 Comments

why not overriding toString to return name ??
Returns what? ONE or one, there is a difference (the enum name or its value)
How to print "One", "Two", I mean the value String for the enum?
9

You can add this method to your Status enum:

 public static String getStringValueFromInt(int i) {
     for (Status status : Status.values()) {
         if (status.getValue() == i) {
             return status.toString();
         }
     }
     // throw an IllegalArgumentException or return null
     throw new IllegalArgumentException("the given number doesn't match any Status.");
 }

public static void main(String[] args) {
    System.out.println(Status.getStringValueFromInt(1)); // OUTPUT: START
}

Comments

0

I believe enum have a .name() in its API, pretty simple to use like this example:

private int security;
public String security(){ return Security.values()[security].name(); }
public void setSecurity(int security){ this.security = security; }

    private enum Security {
            low,
            high
    }

With this you can simply call

yourObject.security() 

and it returns high/low as String, in this example

Comments

0

You can use custom values() method:

public enum SortType { Scored, Lasted;

     public int value(){
         return this == Lasted ? 1:0;
     }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.