Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
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?
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what page did you find this?
 
Oprah Thaddeus
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
 
Oprah Thaddeus
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]
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you quote the exact statement?? Maybe you interpreted it wrong...
 
Oprah Thaddeus
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 :-)
 
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.
 
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
 
Oprah Thaddeus
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.

 
Greenhorn
Posts: 6
Hibernate Spring Java
  • 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.
 
this is supposed to be a surprise, but it smells like a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic