0

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 ?

3 Answers 3

1

Numbers are invalid as identifiers in Java. Typically uppercase letters are used when defining enum constants

Sign up to request clarification or add additional context in comments.

Comments

1

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) returns true.

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.

Comments

0

No, you can't use numeric as the enum names. Maybe you can do

public enum Region {
  ONE("region1"),
  TWO("region2");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.