Dori Dori - 2 months ago 12
Java Question

Best way to create enum of strings?

What is the best way to have a

enum
type represent a set of strings?

I tried this:

enum Strings{
STRING_ONE("ONE"), STRING_TWO("TWO")
}


How can use them as
Strings
?

Answer

I don't know what you want to do, but this is how I actually translated your example code....

/**
 * 
 */
package test;

/**
 * @author The Elite Gentleman
 *
 */
public enum Strings {
    STRING_ONE("ONE"),
    STRING_TWO("TWO")
    ;

    private final String text;

    /**
     * @param text
     */
    private Strings(final String text) {
        this.text = text;
    }

    /* (non-Javadoc)
     * @see java.lang.Enum#toString()
     */
    @Override
    public String toString() {
        return text;
    }
}

Alternatively, you can create a getter method for text.

You can now do Strings.STRING_ONE.toString();