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