MAYA MAYA -3 years ago 89
Java Question

Add date with special character in enum

I try to enum date with special character like this :

public enum Myenum implements enumTypes
{
TO, '2015-01', '2015-02'
}


But I got an error:
Invalid character constant
in
2015-01
and
2015-02
.

I would like to know how to enum with special character.

Answer Source

You can't. Java enum values have to follow the rules for Java identifiers as described in Java Language Specification version 8, section 3.8 Identifiers:

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

Identifier:
   IdentifierChars but not a Keyword or BooleanLiteral or NullLiteral
IdentifierChars:
   JavaLetter {JavaLetterOrDigit}
JavaLetter:
   any Unicode character that is a "Java letter"
JavaLetterOrDigit:
   any Unicode character that is a "Java letter-or-digit"

This means that a single quote (') or minus (-) is not allowed (and a lot of other characters that are not a letter or digit), nor can an identifier start with a number.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download