Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Why final object value can be change whereas final variable value can't?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since final object value can be changed . If this would have been true then the following code would have run fine...

here's the code



If the final object can be changed then kindly give some example for the same.


Thanks in advance.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't change a final variable once it's assigned to. But that's not what you're doing here. n is a final variable, and is still pointing to the same object as it always was. But that doesn't mean that the state of that object can't change, which is what you're doing when you change n.x.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew, I think she's also asking why is it that the wrapper object of type Integer cannot change its state (in this case number) if its set as final since the number variable is an instanceof object, but the class object can change its state. You've already explained the post question for us pretty nicely

Could you elaborate on what I thought she was also asking about?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're probably right - I misunderstood the question.

In which case, the answer is "because Integers are immutable".
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matthew Brown wrote:You're probably right - I misunderstood the question.

In which case, the answer is "because Integers are immutable".



ahhhh YES! Thank you thank you
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Catherine austin wrote:Since final object ...


Hold it right there. Objects aren't ever final. Variables can be.
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Darryl Burke wrote:

Catherine austin wrote:Since final object ...


Hold it right there. Objects aren't ever final. Variables can be.



The english isn't very understandable in her post, but I believe she was referring to reference variables of objects based on the code she showed.

Question about what you said though...wouldnt declaring say an immutable "object" to be final be an example contridicting what you just said? This would be the only case of course as all other uses of the keyword final pertain to methods, classes, fields, and variables.

 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic