wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compile time constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compile time constants" Watch "compile time constants" New topic
Author

compile time constants

Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
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?
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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.


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Thankyou Jess
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
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
sky su
Greenhorn

Joined: Apr 19, 2006
Posts: 6
Hi!!
I just want to say !It's a good question! It's very useful for me!
Thx!
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
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
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
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.


Kiran Kumar.
Naveen Kollipara
Ranch Hand

Joined: Feb 20, 2006
Posts: 32
Thanks Kiran Good answer and good justification


Regards,<br />Naveen<br />SCJP 5 - 83%<br />SCWCD - 84%
Ravinder Singh
Ranch Hand

Joined: Mar 06, 2006
Posts: 54
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

Joined: Mar 02, 2006
Posts: 176
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.
 
Don't get me started about those stupid light bulbs.
 
subject: compile time constants
 
Similar Threads
assignment problem
doubt regarding switch() case construct for flow control
What is the meaning of compile time constants?
Java compile time constants confusion
compile time constant