• Post Reply Bookmark Topic Watch Topic
  • New Topic

Manipulating final variable  RSS feed

 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to head first servlet and jsp p202,

...even a final variable can still be manipulated unless it's immutable...


how can a final variable be manipulated? And what is an immutable variable?
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be the below description answers.

Consider the following classes.


The code in Employee.java is


Now as the value of name can be changed (using setMethod), emp though declared final can be manipulated.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi


if you use "final" keyword, then you just make the reference final, that is you will not be able to re-assign the reference. if your final reference points to object X, then you wont be able to make this reference point to object Y.
but if you can change the STATE of the object X (because the object is mutable) then the final keyword wont stop you from doing it.

 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't care for the terminology in the original quote. Those examples given in the replies all manipulate the object, not the variable. But I'm sure that's what the original quote had in mind.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!