Adding quoted value to Enum

Viewed 1207

I created Enum with additional quoted in string value. When i call this value from Enum in any class i got value, but without quotes.

My Enum:

public enum ValueMapping {
 BSE_M("BSE_MEDIUM"),
 REPS("\"REPS\""),
 TBOMD("TBOMD");

 private String name;

 ValueMapping(String name) {
     this.name = name;
 }

 public String getName() {
     return name;
 }

 public void setName(String name) {
     this.name = name;
 }

 public static ValueMapping getByName(String name) {
     ValueMapping result = null;
     for (ValueMapping operName : values()) {
         if (operName.getName().equals(name)) {
             result = operName;
             break;
         }
     }
     return result;
 }
}

How i try to get value from Enum:

ValueMapping.REPS.name()

Question: Is any way to get value from Enum with quotation marks?

2 Answers

You can use the getName() as:

ValueMapping.REPS.getName()

for e.g.

System.out.println(ValueMapping.REPS.getName());

would print "REPS"

There is no mechanism to override name() because the method is final. And you can't add quotation marks to the name of an enum variable.

So, the only way to get "REPS" is to call ValueMapping.REPS.getName().

Related