Best way to create enum of strings? [java]


Custom String Values for Enum


The default string value for java enum is its face value, or the element name. However, you can customize the string value by overriding toString() method. For example,

public enum MyType {
  ONE {
      public String toString() {
          return "this is one";

  TWO {
      public String toString() {
          return "this is two";

Running the following test code will produce this:

public class EnumTest {
  public static void main(String[] args) {

this is one
this is two


What is the best way to have a enum type represent a set of strings?

I tried this:

enum Strings{

How can I then use them as Strings?

How can I declare enums using java

public enum MyEnum {
   private int value;
   private MyEnum(int value) {
      this.value = value;
   public int getValue() {
      return value;

In short - you can define any number of parameters for the enum as long as you provide constructor arguments (and set the values to the respective fields)

As Scott noted - the official enum documentation gives you the answer. Always start from the official documentation of language features and constructs.

Update: For strings the only difference is that your constructor argument is String, and you declare enums with TEST("test")

Quite simply as follows:

 * @author The Elite Gentleman
public enum MyEnum {
    ONE("one"), TWO("two")
    private final String value;

    private MyEnum(final String value) {
        this.value = value;

    public String getValue() {
        return value;

    public String toString() {
        // TODO Auto-generated method stub
        return getValue();

For more info, visit Enum Types from Oracle Java Tutorials. Also, bear in mind that enums have private constructor.

Update, since you've updated your post, I've changed my value from an int to a String.
Related: Java String enum.

Enums limit you to the required set of inputs whereas even if you use constant strings you still can use other String not part of your logic.

This helps you to not make a mistake, to enter something out of the domain, while entering data and also improves the program readability.

Additionally you can always use your enums as a String if you desire. Here is a reference.

Special characters in an enum

You could do something like this:

private enum SpecialChars{

   private String value;
   private SpecialChars(String value)
      this.value = value;

   public String toString()
      return this.value; //will return , or ' instead of COMMA or APOSTROPHE

Example use:

public static void main(String[] args)
   String line = // a line from STDIN

   //check for special characters 
      || line.equals(SpecialChars.APOSTROPHE)
      || line.equals(SpecialChars.OPEN_PAREN) 
      || line.equals(SpecialChars.CLOSE_PAREN)
   ) {
        //do something for the special chars

You should use something like this instead:

private enum SpecialChars {

   char c;

   SpecialChars(char c) {
     this.c = c;

   public char getChar() {
     return c;