This week's book giveaway is in the Other Open Source APIs forum. We're giving away four copies of Storm Applied and have Sean Allen, Peter Pathirana & Matthew Jankowski on-line! See this thread for details.
In chap-5,pg324 of K&B it is given that "a case constant must evaluate to the same type as the switch expression can use,with one big constraint:the case constant must be a compile time constant!Since the case argument has to be resolved at compile time,that means u can use only a constant or final variable that is assigned a literal value.It is not enough to be final, it must be a compile time constant"For Example:
I am not able to understand what is a compile time constant...here why is b not considered a compile time constant...and how do we come to know which is a compile time constant Plz help! [ April 03, 2006: Message edited by: nivi zal ]
This is the full definition of compile-time constant according to the Java Language Specification.
A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
* Literals of primitive type and literals of type String (�3.10.5) * Casts to primitive types and casts to type String * The unary operators +, -, ~, and ! (but not ++ or --) * The multiplicative operators *, /, and % * The additive operators + and - * The shift operators <<, >>, and >>> * The relational operators <, <=, >, and >= (but not instanceof) * The equality operators == and != * The bitwise and logical operators &, ^, and | * The conditional-and operator && and the conditional-or operator || * The ternary conditional operator ? : * Parenthesized expressions whose contained expression is a constant expression. * Simple names that refer to constant variables (�4.12.4). * Qualified names of the form TypeName . Identifier that refer to constant variables (�4.12.4).
Compile-time constant expressions are used in case labels in switch statements (�14.11) and have a special significance for assignment conversion (�5.2). Compile-time constants of type String are always "interned" so as to share unique instances, using the method String.intern.
A compile-time constant expression is always treated as FP-strict (�15.4), even if it occurs in a context where a non-constant expression would not be considered to be FP-strict.
This is how they define a constant.
We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (�15.28) a constant variable.
u wont believe,i had this doubt haunting my mind for quite sometime,until i tried out a few codes..u call a variable a compile time constant if the compiler knows the value of that variable during compilation rather than at runtime as in most other cases..from ur code,
final int a=1: //compile time constant cos the compiler knows its value during compilation
final int b; //all the compiler knows is tht it is a final variable ,,the value is assigned only during runtime if u do not assign it in the same line it is declared..
b =2; // no use,compilation is over..so b gets the value only at runtime..
Since there is a limitation to switch case variables that they be final and int/int compatible,the compiler should be able to resolve its value at compile time itself to prevent compilation error.