0

I have a switch case label for the enum constants. My Enum and case label example is as follows :

private Enum PreferenceType {
   FIRST,SECOND,THIRD;

   private String prefKey;

   PreferenceType(String prefKey) {
      this.prefKey = prefKey;
   }

   @Override
   public String toString() {
      return prefKey;
   } 
}

private String getPreference() {
   switch(getMessage())
   {
    case PreferenceType.FIRST.toString():
       //do something;
       break;

    case PreferenceType.SECOND.toString():
       //do something;
       break;

    case PreferenceType.THIRD.toString():
       //do something;
       break;
    default: break;
    }
 }

The whole case label statement is marked in red line by eclipse "case PreferenceType.FIRST.toString():" and when I hover the mouse over the case label the error says "case expressions must be constant expressions". My jdk is of version 1.7 and my eclipse is of version 3.7.2. Can someone please tell me how do I resolve this issue?

josaphatv
  • 633
  • 4
  • 19
Sangli
  • 373
  • 4
  • 20
  • Probable duplicate of http://stackoverflow.com/questions/4742660/a-switch-java-problem-case-expressions-must-be-constant-expressions – Alexandre FILLATRE May 09 '14 at 05:35
  • ***Search*** for the *error message*; this is not a new compiler error and you are not the first one to encounter it. – user2864740 May 09 '14 at 05:35
  • See also http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java?rq=1 , http://stackoverflow.com/questions/4742660/a-switch-java-problem-case-expressions-must-be-constant-expressions?lq=1 , http://stackoverflow.com/questions/9092712/switch-case-statement-error-case-expressions-must-be-constant-expression?rq=1 – user2864740 May 09 '14 at 05:37
  • 1
    My question is different. I would want to know why the case label throws an error even though Eclipse 3.7.2 supports Java 7 features. Also the answers that have been mentioned already is nowhere related to my code people so please stop marking it as a duplicate. I'm exhausted working on this issue and someone please help me out here. – Sangli May 09 '14 at 05:39
  • In your enum, the constructor takes an argument, but your enum definitions (FIRST, SECOND, THIRD) don't provide one. Can you please post the real code. – Jason May 09 '14 at 06:34

2 Answers2

1

You can switch on an enumeration, but you cannot switch on a String.

Try:

private Enum PreferenceType {
    FIRST("key1"),SECOND("key2"),THIRD("key3");

    static Map<String, PreferenceType> prefMap = new HashMap<String, PreferenceType>();

    private String prefKey;

    PreferenceType(String prefKey) {
        this.prefKey = prefKey;
        prefMap.put(prefKey, this);
    }

    @Override
    public String toString() {
        return prefKey;
    }

    public static PreferenceType getPreferenceTypeFor(final String key) {
        return prefMap.get(key);
    } 
}

and the switch should be...

switch(PreferenceType.getPreferenceTypeFor(getMessage()))

and your case statements would be...

case PreferenceType.FIRST:
    //do something;
break;

case PreferenceType.SECOND:
    //do something;
break;

case PreferenceType.THIRD:
    //do something;
break;

Now when you call PreferenceType.getPreferenceTypeFor("key1") you will get PreferenceType.FIRST which can then be used in the switch.

Jason
  • 11,744
  • 3
  • 42
  • 46
  • That does not work. Is there any other workaround? Nevertheless thanks – Sangli May 09 '14 at 06:17
  • Edited to handle your use of the prefKey in the switch. – Jason May 09 '14 at 06:38
  • When I use a break statement for every case label mentioned above, Eclipse says its an unreachable code. What's the reason behind this? I have a return statement and then a break after that before the start of next case label such as case PreferenceType.SECOND : return something; break; – Sangli May 09 '14 at 12:17
  • A return statement will force execution to break out of the method - therefore the break statement is not reachable. You don't need a break statement directly after a return statement. – Jason May 11 '14 at 22:38
1

The error message says it all -- case expressions must be constant expressions

You're using the return value of the toString() method, which, as far as the compiler is concerned, is NOT CONSTANT.

josephus
  • 8,284
  • 1
  • 37
  • 57
  • When I use a break statement for every case label mentioned above, Eclipse says its an unreachable code. What's the reason behind this? I have a return statement and then a break after that before the start of next case label such as case PreferenceType.SECOND : return something; break; – Sangli May 09 '14 at 12:16
  • it already says why -- because it's unreachable code. you're already returning from your method with the return statement, anything else you write after that is unreachable. just remove the breaks. – josephus May 10 '14 at 20:52