• 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

changing a final variable

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Er... is 2 - 1 changing the 2?
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic