• 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

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

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic