Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variables can be manipulated ...found this in SCWCD book by Bert!

 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok! This question is not specific to Servlets . However, I came across this while reading the SCWCD book by Bert....

It states that final variable in Java can be manipulated.....

Can you give me an example. I always thought that final means dead end. I mean that is it! It can not be changed as long as the scope of the varibale remains. When the scope dies, the GC might come in and destroy the variable. Now I know that it can be manipulated. I learn better with siimple small code.. PleasE?
 
Ankur Jain Kothari
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what page did you find this?
 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur Jain Kothari wrote:what page did you find this?


page 202... of 1st edition!
 
Ankur Jain Kothari
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then which topic?i have 2nd edition
 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur Jain Kothari wrote:then which topic?i have 2nd edition


in 2nd edition it is on page 204....

final are not final.jpg
[Thumbnail for final are not final.jpg]
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you quote the exact statement?? Maybe you interpreted it wrong...
 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Can you quote the exact statement?? Maybe you interpreted it wrong...


I think I might have interpreted it wrong.. ANyhow, I just put up an image with the statement there.. Please check! Thanks :-)
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final variable can be changed, if you are talking about the variables of any object type.
Means we can change the members of that final variable but we can not change the reference.

For example suppose we have below class:



and we have declared it final as:



Even this variable is final but still be can change the value of variable i like



but we can not change the reference like:



means we can not reassign the object.

Final means we can not change the value of the variable and in case of object variable this value is always a reference to an object on heap. So here we can not change the reference but still it is legal to change the underlying variable states.
 
vinayak chobe
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it means that If a final variable holds a reference to an object then state of the object may be changed but the variable will always refer to the same object
 
Swagato Bhatta
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harpreet Singh janda wrote:Final variable can be changed, if you are talking about the variables of any object type.
Means we can change the members of that final variable but we can not change the reference.

For example suppose we have below class:



and we have declared it final as:



Even this variable is final but still be can change the value of variable i like



but we can not change the reference like:



means we can not reassign the object.

Final means we can not change the value of the variable and in case of object variable this value is always a reference to an object on heap. So here we can not change the reference but still it is legal to change the underlying variable states.


Honestly! I never thought this deep! Wow! Thanks
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, vinayak chobe you are right.

 
Jayant Kaushal
Greenhorn
Posts: 6
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is such an amazing language.
Anything is possible,You just have to find a way.
Nice post guys. Enjoyed reading.
Keep it up.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic