This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

changing a final variable  RSS feed

 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er... is 2 - 1 changing the 2?
 
Yogesh Chhawasaria
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!