Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unexpected Token – Java Enum

Is there any option in java to Create an enum with true and false like below,

public enum options {
true,
false,
both
}

Now getting unexpected token error as I am using true and false. thank you

Regards
haru

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

No. From JLS 8.9.1, an enum constant is defined in the syntax to be

EnumConstant:
    {EnumConstantModifier} Identifier [( [ArgumentList] )] [ClassBody] 

So it’s an Identifier. And from JLS 3.8, and Identifier is defined to be

Identifier:
    IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral 

Hence, an identifier is any valid string of identifier characters (basically letters and numbers, but with Unicode support thrown in) that is not a keyword (like if) or the words true, false, or null.

Realistically, you should be capitalizing your enum names anyway, so it would look more like

public enum Options {
  TRUE, FALSE, BOTH
}

which poses no issues as TRUE and FALSE aren’t Boolean literals in Java.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading