15.28 Constant Expression
A compile-time constant expression is an expression denoting a value of primitive type or a String that is composed using only the following:
Literals of primitive type and literals of type String 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 ? : Simple names that refer to final variables whose initializers are constant expressions Qualified names of the form TypeName . Identifier that refer to final variables whose initializers are constant expressions
4.12.4 final Variables
A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.
[BLAH BLAH BLAH]
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. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).
ash aj wrote:This is a good detailed information..which i was looking for. Last four points related to "To be a variable that is a compile time constant, the variable needs to be... " are very helpful.
Raghu Devatha wrote:I am sorry, new to Java Ranch, do we store these kind of articles elsewhere or this thread will be the only source, cuz this is really helpful.
Did you mean one of the options or all of them?
Henry Wong wrote:
The last part of the definition is the relevant part (I still find it amazing that this is that well hidden in the specification). To be a variable that is a compile time constant, the variable needs to be...
declared as final have a primative or String type initialized (on the same line as the declaration) assigned to a compile time constant expression
All of them.as for example:
Maor wrote:Did you mean one of the options or all of them?
Bruno Pinheiro wrote:I'm only asking this, because i can't find anything to help(not even in Java Spec). I'm not looking for the rule which says "initialize in th same line as the declaration", i'm looking fo something like "when you try to assign after the declaration, the compiler will undestand that and that and blah blah blah".
Campbell Ritchie wrote:Welcome to the Ranch
Remember that your switch is translated by the javac tool into a lookup table in the bytecode.
Bruno Pinheiro wrote:
Afraid that is incorrect. It is declared as a final variable, and that means it must be assigned to once and once only. You do not have to assign on declaration, nor can you reassign varB.
varB=7; this code determined at runtime, which means in future the value of varB may change, like
varB = 9; so value may change that's why it is not final variable, if you want final variable it must be initialized at the time declaration so that it's value should not change
Afraid it doesn't help.
Hope this helps.....
Yes, I know it has to be a compile time constant; I wrote that myself two years ago.
sohail hussain wrote:. . .
in switch statement , case varB:// error says case must be compile time constant ,so that its value should not be change,
your are welcome, i will come directly to the point
Campbell Ritchie wrote:Sorry, I was claiming credit for something somebody else wrote. Corrected the name in the quote to the person who posted that code before I did.