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?