Hi, can anyone please tell me what are compile time constants? 1>final int a=10; //Is this a compile time constant? 2>int b=0;//Is this a compile time constant? 3>Is it like,all variables that are marked final are compile time constants?
Hi, Yes you got it right putti, Variables that are marked as final are called as Compile Time Constants. Thease are constants because once assigned their value cannot be changed.
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Wait! !! Can you guys tell me the difference between "a constant" and "compile-time constant" please? Coming to final variables, they say values ONCE INITIALIZED can not be changed. So you are safe until you have not assigned any value. And hence Seb's code...
Compiles fine. But if I place another statement...
i=2;
Right below the line 1, the compiler alarms... with,
variable i might already have been assigned i=2; ^
I just say that final variables are not always compile-time constants.
In my example above, i is a final variable, which must must be definitely assigned at the end of every constructor of the class in which it is declared
In the expression static final int j= 2*3;
The compile-time constant is 2*3, which is assigned to j.
Thanks Seb, I got the difference between constants and compile-time constants. Yes, I do support your point that final variables may be compile time constants and that doesnt mean " every variable that is declared final is a compile time constant. [ October 05, 2005: Message edited by: Akhil Trivedi ]
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?