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
>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.