Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (2.6k points)

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 I then use them as Strings?

1 Answer

0 votes
by (46k points)

I don't understand what you require to do, but this is how I altered your example code...

/**

 * 

 */

package test;

/**

 * @author The Elite Gentleman

 *

 */

public enum Strings {

    STRING_ONE("ONE"),

    STRING_TWO("TWO")

    ;

    private final String text;

    /**

     * @param text

     */

    Strings(final String text) {

        this.text = text;

    }

    /* (non-Javadoc)

     * @see java.lang.Enum#toString()

     */

    @Override

    public String toString() {

        return text;

    }

}

Alternatively, you can generate a getter purpose for text.

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

Related questions

0 votes
1 answer
0 votes
1 answer
asked Aug 13, 2019 in Java by Nigam (4k points)
0 votes
1 answer
asked Aug 7, 2019 in Java by Nigam (4k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...