Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

compile time constant

Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 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 ]
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 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.

hope i did not confuse u further..!
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
    Bookmark Topic Watch Topic
  • New Topic