3

I have a problem in creating an enumeration in Java using "-"-separated strings:

public enum CipherList{

   RSA-MD5,AES128-SHA,AES256-SHA;
}

I am getting compilation errors.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Style
  • 49
  • 3
  • 2
    Why do you need the dashes in the identifiers? Because I think you'd need something [like this](http://stackoverflow.com/a/9781903/737620) – wrock May 23 '12 at 07:18
  • `-` is not valid inside an identifier (as it would be confused by the minus sign / substraction operation. Anyway next time provide more information about the error if you expect some reply – SJuan76 May 23 '12 at 07:20

3 Answers3

9

The - symbol may not be used in an identifier in Java. (How would RSA-MD5 be parsed if RSA and MD5 happened to be integers?)

I suggest you use

RSA_MD5, AES128_SHA, AES256_SHA;

according to the Java coding conventions for constants related question.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
3

Enum constants have to use valid Java identifiers, and identifiers are not allowed to contain dashes.

You could, for example, replace dashes with underscores:

public enum CipherList{
   RSA_MD5, AES128_SHA, AES256_SHA;
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

If you want to use exact strings using enums you can use following approach.

enum CipherList{
   CHIP_ONE("RSA-MD5"),CHIP_TWO("AES128-SHA"),CHIP_THREE("AES256-SHA");
   private String code;
   CipherList(String code) {
      code= code;
   }
   String getcode() {
      return code;
   } 
}
Eshan Sudharaka
  • 607
  • 2
  • 9
  • 16