Reputation: 815
Due to an unavoidable circumstance, I need to create an enum class like below,
public enum Region
{
1("Region1"),
2("Region2");
}
But I'm getting an error as "Syntax error on token '1', identifier expected". This enum is used in an option tag in jsp. It works fine if use a string instead of 1, enum doesn't allow numeric as keys ?
Upvotes: 0
Views: 1509
Reputation: 137064
The first character of any identifier has to be a letter. From the JLS section 3.8 (emphasis mine):
An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.
[...]
A "Java letter" is a character for which the method
Character.isJavaIdentifierStart(int)
returnstrue
.The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.
Digits are not allowed, so you can't name your enum values 1
and 2
.
Upvotes: 1
Reputation: 159754
Numbers are invalid as identifiers in Java. Typically uppercase letters are used when defining enum constants
Upvotes: 1
Reputation: 5134
No, you can't use numeric as the enum names. Maybe you can do
public enum Region {
ONE("region1"),
TWO("region2");
}
Upvotes: 0