Reputation: 1929
I have this:
import java.util.ArrayList;
public class MyClass {
ArrayList list = new ArrayList();
public MyClass(){
}
public MyClass(MyClass aClass){
this.list = aClass.getList();
}
public void add(int number){
list.add(number);
}
public void set(int number){
list.set(0, number);
}
public int get(){
return (Integer)list.get(0);
}
public ArrayList getList(){
return list;
}
}
MyClass aName = new MyClass();
aName.add(5);
System.out.println("aName: "+aName.get());
MyClass aName2 = new MyClass(aName);
System.out.println("aName2: "+aName2.get());
aName2.set(1);
System.out.println("aName2: "+aName2.get());
System.out.println("aName: "+aName.get());
This will print: aName: 5 aName2: 5 aName2: 1 aName: 1
I don't want my second object changing the values in my first object. Is there any way to stop this happening but still be able to copy properties from another object?
Upvotes: 0
Views: 95
Reputation: 24895
Then don't pass the List from the original object to the new object, in
public MyClass(MyClass aClass){
this.list = aClass.getList();
}
It makes both object have a reference to the same list (there is only one list shared between both objects).
You should do
public MyClass(MyClass aClass){
this.list = (List) aClass.getList().clone();
}
Upvotes: 1
Reputation: 76918
You're copying the reference value of the ArrayList
into your new object. You now have two references to the same list.
You need to create a new ArrayList
when you construct your second object:
public MyClass(MyClass aClass){
this.list = new ArrayList(aClass.getList());
}
Be aware, however, that both lists now contain the same objects. Again, this only creates a new list and copies the reference values of the objects inside the original list. If you needed two complete and separate lists containing unique objects you would need to write a method that did a deep copy, duplicating the objects in the list as well.
Upvotes: 0
Reputation: 533820
You could clone() or take a copy, but since you have created a list already, rather than discarding it you could use it. ;)
public MyClass(MyClass aClass){
this.list.addAll(aClass.getList());
}
Upvotes: 1
Reputation: 82589
Yes you can stop this. You need to make defensive copy of the list in your copy constructor.
public MyClass(MyClass aClass){
this.list = new ArrayList(aClass.getList());
}
You could use clone()
, but as Josh Bloch states in Effective Java item "copy constructors have many advantages over clone
" - they're preferable in practice.
Upvotes: 1