• 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 constants

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across the the book by Kathy Sierra and Bert Bates that
in switch(){ case a: ... }
'a ' should be a compiler constant.
Example :

final int a =3;
final int b;
b=2;
switch(4)
{
case a:
// some code

case b:
// some code
}
Answer : compiler error .. 'b' is not a complie time constant..
My doubt is : both 'a' and 'b' are declared final and also initialized.. with 3 and 2 respectively..
I am confused ... what is a complie time constant?

what are compile time constants?
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
compile time constant means that at compile time -- the compiler knows what the value of the constant is.
You could have had something like this:
final int a =3;
final int b;
b=a+2/23-x;
and the value of b wouldn't be known right away... so the compiler wouldn't know how to set up the switch statement.
 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou Jess
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jessica,

Sorry for the late entry..however since I am also reading Kathy & Bert's book for SCJP 5 exam and have just read the same code snippet that Rajani wrote sometime back.

code:


Based on your earlier reply, isn't compiler aware that final var b is assigned an int literal?
I have read JLS 15.25 on Constant Expressions http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5313 but quite understand in relation to this code.

Secondly, why are final static wrapper variables aren't considered constants?

Would appreciate your reply...

------------
Ravinder
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!
I just want to say !It's a good question! It's very useful for me!
Thx!
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b is local final blank
and the blank final denoted the value of b determining in run time
so blank final is not constant exprssion

form jls3

so the final wrapper class is not constant exprssion
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravinder, if you say wrapper classes, they will have objects, we can make them also final so that only one time reference to an object is assigned but its state can be changed,as wrapper classes are immutable so its state can not be changed so it is acting like final basic data type variables itself.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kiran Good answer and good justification
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies.

Kiran Kumar's justification seems to look valid for the question: "Why final static wrapper variables are not considered constants at compile-time?"

Again, here is my understanding of what Kiran meant. Pls. correct if I'm wrong..
Wrapper objects are NOT considered constants because you can STILL manipulate its values as objects by invoking the respective wrapper class' methods,.. even though you CANNOT change the object it refers to by being final.

With respect to the following code, I still haven't quite understood why final var b is not a compile-time constant?


Pls. help me with this..

----------------
Ravinder
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code:
--------------------------------------------------------------------------------

final int a =3;final int b;b=2; switch(4){case a: // some code case b:// some code }


if we declare a field as final variable we have to initialize either like

final int b=1;
or

final int b;

{
b=1;//this is instance initializer block
}

or
we can initialize it in the class's constructor, (i am telling this for instance variables),failing which complier will flag it as an error.

you might have not done any of the above cases that is why compiler flaged it as an error.
 
reply
    Bookmark Topic Watch Topic
  • New Topic