aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compile Time constant?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compile Time constant??" Watch "Compile Time constant??" New topic
Author

Compile Time constant??

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
What is a Compile time constant??

What is the difference between a,b??
final int a=1;
final int b;
b=2;
int x=0;
switch(x)
case a:
case b://complier error.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Abhi,
Compile time constant is a constant value that the compiler assigns to the variable during compile time itself.

final int a=1; -- This is a compile time constant because during compile time itself, a value of 1 is assigned to 'a', and since it is final, it cannot be changed further.

final int b -- During compile time, this statement only declares 'b', no value is assigned. During runtime only, the value 2 is given to 'b'.

switch-cases expect compile time constants in the case statements. But in your code, since you have used a non-compile time constant for case statement, the compiler error is given out.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks, a lot,Rekha.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Compile Time constant??