Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - How to add special character in enum?

Tags:

java

enums

I have a problem here. I created SpecialCharacterField.java - an enum class that will list some special characters.

SpecialCharacterField.java

package bp.enumfield;

public enum SpecialCharacterField {
   +, #;
}

In my eclipse on the line : public enum SpecialCharacterField{ there is an error it says: Syntax error, insert "EnumBody" to complete EnumDeclaration

Please help. Thanks in advance.

like image 337
NinjaBoy Avatar asked Jul 12 '12 04:07

NinjaBoy


2 Answers

do something like this,

public enum SpecialCharacterField{
   PLUS("+"),
   HASH("#");

   private String value;
   private SpecialCharacterField(String value)
   {
      this.value = value;
   }

   public String toString()
   {
      return this.value; //This will return , # or +
   }
}
like image 57
Kushan Avatar answered Oct 25 '22 16:10

Kushan


Those characters cannot be parts of identifiers in the Java language. Note that the JVM itself imposes no such restrictions (only ./; and [ are prevented), so you could use names like that if you wrote bytecode directly. However this is usually not a desirable approach.

like image 41
Antimony Avatar answered Oct 25 '22 16:10

Antimony