• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing the value of a wrapper object  RSS feed

 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appendix B of HF Java says, "When you create a wrapper object like: Integer iWrap = new Integer(42); That's it for the wrapper object. It will ALWAYS be 42. There is no setter method for a wrapper object. You can refer iWrap to a different wrapper object, but then you'll have two objects.



What I don't understand is I was able to change the value of iWrap. It was not ALWAYS 42!!! Can someone explain what I'm missing?
Thank you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have not changed anything except which object the reference is pointing to. Line 2 creates a new wrapper object.

Exactly as stated.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create a wrapper object like: Integer iWrap = new Integer(42); That's it for the wrapper object. It will ALWAYS be 42. There is no setter method for a wrapper object. You can refer iWrap to a different wrapper object, but then you'll have two objects.


The part in bold is what you just did. You let iWrap refer to a different wrapper object. You didn't set the original object to a different value, you just discarded it and created a new wrapper object with the value 43. Recall that there's a difference between variables and objects. The text from HF says that you can change the Integer variable, but not the object.
 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. The explanations let me see what I overlooked in black and white. Thanks.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!