1

Is there a workaround to be able to use colons as the key in enums?

public enum foo {
   _DEFAULT_GET("com.foo.my.package");   // works fine
   _PREFIX_GET("com.foo.my.other.package");
   _PRE:CODE_GET("com.foo.yet.another.package");  // <-- how do I escape this colon?
}

EDIT: before I get downvoted into oblivion, I'd like to add that there was a naming convention change that was handed down. It has caused quite a fun debate in the team! :-)

It Grunt
  • 3,300
  • 3
  • 21
  • 35

5 Answers5

9

No, : is not a legal character in a Java identifer.

The legal characters are, a-z A-Z, 0-9, (the {unicode letters}), _ and $

You're trying to do the same as

private String he:llo; // not valid since : is a language construct, used in places such as labels

Also your program shouldn't depend on the names you give to the fields. Unless you do reflection; and you shouldn't need that, either...

Note that you shouldnt use the $ in your identifiers since its used by code-generators mostly, such as javac when it compiles a class containing an inner class $ is used as separator.

As @Kevin.K mentioned, a-zA-Z is actually unicode letter code.

rapadura
  • 5,242
  • 7
  • 39
  • 57
  • 2
    the currency character('$') is also legal I believe – Zack Macomber Jan 17 '12 at 22:00
  • 2
    Almost any Unicode character is legal actually, see [this question](http://stackoverflow.com/questions/65475/valid-characters-in-a-java-class-name) – Kevin K Jan 17 '12 at 22:05
  • We decided to strip the colon and replace it with an underscore before we grabbed the value out of the enum. Thanks! – It Grunt Jan 18 '12 at 00:50
4

According to the Java language specification each enum value has to be an identifier:

EnumConstant:

Annotations Identifier Argumentsopt ClassBodyopt

and identifiers can't have colon (:) inside. So no, you can't escape it.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
1

You can't. As a name you can use string containing alphabet characters, digits or underline ( _ ) or dollar ($)
And also note, that the name can not start with a digit. Only with alphabet character, underline or dollar.

shift66
  • 11,760
  • 13
  • 50
  • 83
  • Almost any Unicode character is legal actually, see [this question](http://stackoverflow.com/questions/65475/valid-characters-in-a-java-class-name) – Kevin K Jan 17 '12 at 22:06
1

The rules for identifiers (aka "names") in Java do not allow certain "special" characters, such as colon (:) or semi-colon (;) to be embedded in the identifier.

You can read about it here.

It is a deliberate choice brought about by reducing the possible contexts one needs to consider when seeing such a special character. Semicolons are practically reserved for only end-of-statement delimiters, which colons are reserved for jump points in a switch statement.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

Weird requirement. It is not possible as enum properties are just syntactic sugar over class member variables and variables names cannot be escaped. You can however override the toString method. oven have a custom constructor and getter/setters for additional properties.

Usman Ismail
  • 17,999
  • 14
  • 83
  • 165
  • It was done to help organize future content. Our solution was kind of hack-ish, but I suppose it beats rolling back a lot of DB and code changes. – It Grunt Jan 18 '12 at 00:52