• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compile time constant

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jolly Singh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

this does not compile but if this is written within a method
this compiles.why?
 
Jolly Singh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"jolly good", please check your private messages. You can see them by clicking My Private Messages.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks a lot ashu.
that was very helpful.
 
reply
    Bookmark Topic Watch Topic
  • New Topic