• Post Reply Bookmark Topic Watch Topic
  • New Topic

mutable immutable confusion  RSS feed

 
Mike Mitchel
Greenhorn
Posts: 12
Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was attempting to solve programming exercise 10.2 in Khalidh Mughal book. The exercise was about hashCode() & equals() methods & concept of immutability.

As per my understanding the class Pair is still mutable. I was expecting p1 as

p1 item1 new item2

But what I got was this one

p1 item1 item2

I was expecting that reference at (1) will be able to change the obj1 value in Pair.

Could somebody please explain?



 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike.

The problem is that you're expecting obj1 in p1 and p2 to change, by changing the value of item1.

obj1 is a variable holding a reference to an Item1, and item1 is a separate variable holding such a reference.

When you perform item1 = new Item1("item1 new"); you are changing the value of item1, so that it contains a reference to a new object. This does not magically change the values of any other variables in your program.

In Java, when you pass an argument in a method call, you are really passing a copy of the reference. The variable you're passing will not be magically linked to the method parameter (like Aliasing, in the C++ language).
 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do realise you have not created an immutable class? Because the Item classes have non‑private fields they are mutable. Let's change your main method a bitNow try it again.

There is an answer to that problem, which you can find in sources like Effective Java by Joshua Bloch.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Mitchel wrote:I was expecting that reference at (1) will be able to change the obj1 value in Pair.

In which case the class is NOT immutable.

Tip: If you want a class to be immutable, make ALL of its fields (in your case obj1 and obj2) final, as well as the class itself. If you do that, I suspect the compiler will throw up some errors before you can continue, and they may help to explain what you're doing wrong.

If, on the other hand, you want to make it mutable, then I would suggest that you provide setter methods for those fields to want to allow to be changed (and obviously, remove the final from them).

HIH

Winston
 
Mike Mitchel
Greenhorn
Posts: 12
Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everybody for helping. now I got it.

thanks CR for your example , which cleared up things .

Let me explain my understanding



This one will create a new Object Item1 & assigns it to reference variable item1 which will leave alone the original object.



This one will change member in object referenced by item12.


I was expecting both these scenarios to be similar. my bad. :-(
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reference inside the class and the reference outside the class (originally) pointed to the same object. So any changes in the state of the object outside your class (e.g. item12.name = "Campbell") are reflected inside your object. There is a solution to that problem; have you found it yet?
Another thing is that fields should be marked private, unless you are using them as global constants. If they are global constants, they should be public static final and be of a primitive type or an immutable reference type. String is an immutable class, so the following is a bona fide global constantAnyway, what I did was to change the state of a mutable object which was a field of your class.

What you tried to do was to change the reference. If you change a reference to point to a different object, you do not affect any other references. You thought that changing a reference outside the class would change that inside the class. As Stephan told you, since Java® is entirely pass‑by‑value, that doesn't work.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote: . . . fields should be marked private . . .
I meant the fields inside the item classes, which are not private.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!