Houssem Bdr Houssem Bdr - 1 year ago 80
Java Question

Add slash to an enumeration in Java

I need to create an

like this:

public enum STYLE {
ELEMENT1(0), A/R (2)

does not permit this. Is there any solution?

Answer Source

Enumeration in java is just a compile time syntactic sugar. Actually enum is just a class and enum member is just as public final static field of this class. Each field has name that must follow certain rules. For example name cannot contain special characters that mean something special in the language. For example / cannot be used as a part of name.

You can call it A_R if you want.

If however you want additionally to be able to extract string A/R from enum member A_R you can define method that returns it, e.g.

public enum STYLE {
    A_R (2) {
        public String printableName() {
            return "A_R";

    public String printableName() {
        return name();