2

In Java all variables are passed by value. For objects, this means that we pass reference on object. I am confused with following situation. Let's say that we have a graph of objects. One object contains as field reference to another object, that object has reference to one more object like: user.address.city. Here we have instance of class User, that instance has reference to instance of class Address which has field of type String - city. Now let's say we have 2 users:

User bob = new User(); User mary = new User(); 

Assume object 'bob' has reference to address object:

Address bobAddress = new Address(); bobAddress.setCity("New York"); bob.setAddress(bobAddress); 

Let's say Mary and Bob started to live together in one house and they have the same address, no problem:

mary.setAddress(bobAddress); 

Now mary and bob have reference to one address object. But one day they divorced and mary now has another address:

mary.getAddress().setCity("Boston"); 

And here we have a problem: Mary changed something in her address, but she also changed Bob's address, because they share the same reference.

All text above was my try to describe in small scale a big problem for me: When I should create a copy (clone) of some object before set the reference of that object into another instance? Does some rules or techniques exist? I know about immutable objects (e.g. String, Integer ...) with these objects we don't need to think about such problems at all, but what if I have mutable objects. How I should behave with them?

1
  • You understand the mechanics / language perfectly. But you are right that your design will create this problem and therefore doesn't work. Hence why I upvoted the design correction. Commented Sep 25, 2014 at 20:28

2 Answers 2

7

Your problem indicates bad design of the Address class.

The Address class represents a geographic location and is fixed. When an individual moves, the geographic location does not change; instead, ghe geographic location that the individual inhabits changes.

Remove the setters for Address and make it an immutable object. When an individual moves, you must do one of the following:

  1. If the individual is sharing an existing Address (object instance) with another individual, then assign the existing reference.
  2. If the individual moves to a new Address (a new object instance), then assign the address to a new Address instance.
Sign up to request clarification or add additional context in comments.

2 Comments

probably better worded than my answer +1
@Ajk_P your answer is right but you may not have worked with the concept of immutability yet. That's definitely the way to frame this. It's extremely important in highly concurrent and functional programming. Life gets easier when things that don't change (like an address's city or a date's year) don't change in code either.
1

I think your thinking is off.

If Mary moves, that means that she will go to a new address. Try to put that into Java. You simply need to create another address for Mary.

IMO, you should never clone the same object for assignments if it's the same object. (you could for some processing computations).

Just reduce coupling and have mary.setAddress(String streeName, String City, String State). Normally an address's city SHOULD NOT change as streets cannot move from one city to another, therefore you should not even have a setCity() public method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.