4

I was writing some simple code in java for my android app and i got these errors.

case expressions must be constant expressions while private static final Integer is constant

private static final Integer INVALID_USER = 901;
private static final Integer SENDING_FAILED = 902;
private static final  Integer OK = 903;
/*
 *
 *  And some more project related declaration...
 *
 */


        switch (responseCode){
            case INVALID_USER:

                    // logout
                    break;

            case SENDING_FAILED:

                    //resend request
                    break;

            case OK:
                    break;
        }

This is because i used Integer Type, then i changed type to int and problem is solved

My question is why we cant use Integer as a case expression. Docs says " A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer" though variable is constant I read this question but didnt get any thing

Community
  • 1
  • 1
jaimin
  • 563
  • 8
  • 25

3 Answers3

3

Constant expressions are used as case labels in switch statements ( §14.11 ) and have a special significance for assignment conversion ( §5.2 ) and .....

Definition of Constant Expression §15.28

A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly.

Now in above scenario compiler is looking for constant expression as it should be know to the compiler at the compile time.As stated Integer is actually not a constant Expression for the compiler.

akash
  • 22,664
  • 11
  • 59
  • 87
2

The Docs lines "Docs says : A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer" is for

switch(expressions used here)

and not with case statement.

You can use primitive type int with case and then Integer.valueOf(your_int_value); where ever you want it as a Integer

Julien Kronegg
  • 4,968
  • 1
  • 47
  • 60
Pooja
  • 113
  • 10
-2

java does not allow to use Object into the switch. we can use byte, int, char and enumerated value as a switch expression. in jdk1.7 we can use String as a switch expression. to do this for wraper Class object you need to use valueOf method of wrapper class

private static final Integer INVALID_USER = 901;
private static final Integer SENDING_FAILED = 902;
private static final  Integer OK = 903;
/*
 *
 *  And some more project related declaration...
 *
 */


        switch (responseCode){
            case INVALID_USER.valueOf():

                    // logout
                    break;

            case SENDING_FAILED.valueOf():

                    //resend request
                    break;

            case OK.valueOf():
                    break;
        }