29

I have a enum which looks like:

public enum Constants{
  YES("y"), NO("N")
  private String value;

  Constants(String value){
    this.value = value;
  }
}

I have a test class which looks like

public class TestConstants{
 public static void main(String[] args){
   System.out.println(Constants.YES.toString())
   System.out.println(Constants.NO.toString())
 }
}

The output is:

YES
NO

instead of

Y
N

I am not sure what is wrong here ??

1
  • 2
    why do you expect that toString() returns the value? Commented Feb 1, 2016 at 21:24

6 Answers 6

39

You need to override the toString method of your enum:

public enum Constants{
    YES("y"), NO("N")

    // No changes

    @Override
    public String toString() {
        return value;
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Maybe, maybe not. Maybe the OP needs a separate getValue() method instead?
Maybe, but the OP is explicitly calling the un-overriden toString() method of his enum, which pretty much sums it all.
What is a better practice. Overriding the toString() method or writing a separate getter method ??
getter method is simplier, but both of solutions are ok.
Although this solution works, it only works for this SPECIFIC problem. If a "googler" comes here and the enum he or she needs to obtain the value from has non-String values, this solution will not work. As as common practice, you should not use the toString() to return values. That violates the Object.toString() API. In addition, overriding this method works only for String values. Instead, you should create a getValue() function that returns the appropriate data type. See David Yee's or Just a Logic Gate's answer. They are more correct than this one, regardless of the OPs vote.
|
17

You can also add a getter to the enumeration and simply call on it to access the instance variable:

public enum Constants{
    YES("Y"), NO("N");
    private String value;

    public String getResponse() {
        return value;
    }

    Constants(String value){
        this.value = value;
    }
}

public class TestConstants{
    public static void main(String[] args){
        System.out.println(Constants.YES.getResponse());
        System.out.println(Constants.NO.getResponse());
    }
}

1 Comment

Regardless of the OP's vote, this is the correct solution. See my comment in the "Best Answer" post.
12

Create a getValue() method in your enum, and use this instead of toString().

public enum Constants{
 YES("y"), NO("N")
 private String value;

 Constants(String value){
  this.value = value;
 }
}

 public String getValue(){
  return value;
 }

And instead of:

System.out.println(Constants.YES.toString())
System.out.println(Constants.NO.toString())

(Which are also missing a semi-colon), use

System.out.println(Constants.YES.getValue());
System.out.println(Constants.NO.getValue());

Hope this solved your problem. If you do not want to create a method in your enum, you can make your value field public, but this would break encapsulation.

1 Comment

Regardless of the OP's vote, this is the correct solution. See my comment in the "Best Answer" post.
7

Write Getter and Setter for value and use:

System.out.println(Constants.YES.getValue());
System.out.println(Constants.NO.getValue());

Comments

0
String enumValue = Constants.valueOf("YES")

Java doc ref: https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#valueOf(java.lang.Class,%20java.lang.String)

1 Comment

Why use valueOf? This is more error prone than to use the enum itself and call its getter method.
0

The elegant way to get value of an Enum is by using ordinal method.

public enum Constants
{
  YES("Y"), NO("N");
  private String value;

public String getResponse() {
    return value;
}

Constants(String value){
    this.value = value;
}
public static Constants getConstants(int value){
    for(Constants e : Constants.values()){
        if(value == e.getResponse()) return e;
    }
    return null;
  }
}

public class TestConstants{
public static void main(String[] args)
  {
    System.out.println(Constants.YES.ordinal());
    System.out.println(Constants.NO.ordinal());
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.