aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes changing a final variable 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 "changing a final variable" Watch "changing a final variable" New topic
Author

changing a final variable

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131


I had a doubt on the line "case x-1:..."
afaik, x is final and it is not supposed to be changed. but it seems saying something as "case x-1" is perfectly allright.
can someone explain why mutilating a final variable is allowed ?

TIA
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Er... is 2 - 1 changing the 2?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Yogesh Chhawasaria
Ranch Hand

Joined: Apr 02, 2004
Posts: 53
Originally posted by Netty poestel:


I had a doubt on the line "case x-1:..."
afaik, x is final and it is not supposed to be changed. but it seems saying something as "case x-1" is perfectly allright.
can someone explain why mutilating a final variable is allowed ?

TIA


Netty

First lets go for the rule regarding case argument's i.e. case arguments must be final i.e. the value of the case argument must be known at compile time.

Secondly

case x-1: is allright bcos we are not changing the value of x anywhere.
During compilation the case x-1 is interpreted as
case 2-1 i.e case 1

See nowhere the value of x was changed...
x-1 doesnt (attempt to) change the value of x ... but x++ , x--, ++x,--x does.

Also your above code wont compile since variable y is not final.


When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
thank you
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: changing a final variable