0

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.

Komal12
  • 3,340
  • 4
  • 16
  • 25
MAYA
  • 1,243
  • 1
  • 12
  • 20
  • hint: it doent matter what "TO" is for a type....this is not a char: ***'2015-01'*** – ΦXocę 웃 Пepeúpa ツ Aug 16 '17 at 07:41
  • @robjwilkins TO is a string but I have a problem with my dates. I tried to put it as string in " " but the same problem – MAYA Aug 16 '17 at 07:42
  • 1
    You can't. enum names must be valid Java identifiers. They thus can't start with a quote, nor with a number. – JB Nizet Aug 16 '17 at 07:42
  • @JBNizet I can enum names. I did not have a problem with `TO` but It was for `2015-01`. It is a string but I got error with special character – MAYA Aug 16 '17 at 07:43
  • 1
    Read my comment again. And read the answer from Mark. Enum names are not strings (and BTW, string literals are surrounded by double quotes in Java). They must be valid Java identifiers. Not arbitrary strings. Valid Java identifiers. Just like a field names. They **are** field names, BTW. A correct name would be JANUARY_2015, for example. – JB Nizet Aug 16 '17 at 07:45
  • Related: https://stackoverflow.com/questions/10399207/special-characters-in-an-enum?rq=1 – Mark Rotteveel Aug 16 '17 at 07:47
  • @MAYA You should really [read the tutorial](https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) to have a basic idea of the concepts of enums. – MC Emperor Aug 16 '17 at 07:52

1 Answers1

5

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197