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
解决方案
Constant expressions are used as case labels in switch statements (
§14.11 ) and have a special significance for assignment conversion (
§5.2 ) and .....
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.