File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compile time constant" Watch "compile time constant" New topic
Author

compile time constant

Jolly Singh
Greenhorn

Joined: Aug 31, 2008
Posts: 7
hi guys.I got this doubt when i ws goin thru SCPJ study guide.ch5.pg.no.336.
i want to know first of all , how a final variable is not a compile time constant.secondly the eg givn is this:-

final int a=1;
final int b;
b = 2;
int x = 0;
switch(x)
{
case a: //OK
case b: //compiler error

so how is case a: different from case b:??
and as a last favor tell me how the hell do i copy from the secure pdf that is given with the SCPJ CD!
Jiang zhixiao
Greenhorn

Joined: Jul 01, 2008
Posts: 13
final int a=1;
final int b;
b = 2;
int x = 0;
switch(x)
{
case a: //OK
case b: //compiler error
case expressions must be constant compressions
it means when you define it's type,you must init it's value;
Jolly Singh
Greenhorn

Joined: Aug 31, 2008
Posts: 7
thanks alot.i figured it has to be some thing like tat..so tat means a final variable is not necessarily a compile time constant rite??
Jiang zhixiao
Greenhorn

Joined: Jul 01, 2008
Posts: 13
Originally posted by jolly good:
thanks alot.i figured it has to be some thing like tat..so tat means a final variable is not necessarily a compile time constant rite??


if final variable as a member of a class,it must be inited;

when in a method

hope will help you
(i'm not good at english,from china)
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

no, final variables are not compile time constants unless they are initialized on the same line while they are declared.

for example,

final int a =10 //Compile time constant
final int a // not a compile time constant even though it is final.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Even if you initialize a constant on the line of declaration, in some cases it is not a compile time constant...

int a = 10;
final int b = a; //not a compile time constant


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jolly Singh
Greenhorn

Joined: Aug 31, 2008
Posts: 7
Thanks a lot for your views guys.
although correct me if am wrong,but doesn't final int b=c; mean that the bit pattern of variable 'c' is copied into 'b'??and for primitives the bit pattern is the value of the literal.so doesn't this mean that b is initialized in only one line??so shouldn't it also be a compile time constant?..please comment
ankana mukherjee
Greenhorn

Joined: Aug 22, 2008
Posts: 6

this does not compile but if this is written within a method
this compiles.why?
Jolly Singh
Greenhorn

Joined: Aug 31, 2008
Posts: 7
Originally posted by ankana mukherjee:

this does not compile but if this is written within a method
this compiles.why?


well this has nothing to do with final.even just a simple int instance variable will also give the same error if declared 1st and then initialized in another line which is also inside an instance variable exclusive scope.In general any instance variable, if not initialized when it is declared, then can be done so only with an instance of the that class.or at least that's what i guess...i am also guessing there are cases when this may not be true!
[ September 03, 2008: Message edited by: jolly good ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

"jolly good", please check your private messages. You can see them by clicking My Private Messages.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ashu Jain
Ranch Hand

Joined: Feb 24, 2008
Posts: 73
Compile time constants of final are well explained at following link.

Please go through this links.
http://www.codeguru.com/java/tij/tij0071.shtml
Jolly Singh
Greenhorn

Joined: Aug 31, 2008
Posts: 7
hey thanks a lot ashu.
that was very helpful.
 
wood burning stoves
 
subject: compile time constant