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
You cannot use / to name a Java identifier. You can have a look at the §JLS 3.8 to see what all special characters are allowed in naming of a Java identifier.
For your scenario, you can use an enum which looks like this.
enum STYLE {
ELEMENT1(0, "ELEMENT1"), A_R(2, "A/R");
private int code;
private String name;
private STYLE(int code, String name) {
this.code = code;
this.name = name;
}
// Other methods.
}
/ cannot be used in the name. If your A/R constant denotes "A or R", you can rename it to A_OR_R.
Edit:
If you need to compare it to a String equal to "A/R", you can override the toString() method in the constant.
public enum Style {
A_OR_R(2) {
public @Override String toString() {
return "A/R";
}
}, ELEMENT1(0);
}
Then
String test = "A/R";
boolean isEqual = test.equals(style.A_OR_R.toString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With