Houssem Bdr Houssem Bdr - 4 months ago 33
Java Question

Add slash to an enumeration in Java

I need to create an

enumeration
like this:

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


But
Java
does not permit this. Is there any solution?
Thanks

Answer

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 {
    ELEMENT1(0), 
    A_R (2) {
        @Override
        public String printableName() {
            return "A_R";
        } 
    };

    public String printableName() {
        return name();
    } 
}