If I have a class:
public class MyType
{
private List<Integer> data;
private boolean someFlag;
public MyType(List<Integer> myData, boolean myFlag)
{
this.data = myData;
this.myFlag = someFlag;
}
}
Now, if I create an instance of MyType, how do I make a deep copy of it? I do not want the new object to point to the old link, but a completely new instance.
Is this the case when I have to implement the Cloneable interface, or is it used for small copies?
I can't just do:
MyType instance1 = new MyType(someData, false);
MyType instance2 = new MyType(instance1.getData(), instance1.getFlag());
I'm worried about new instances of MyType pointing to the same link for their "data" variable. Therefore, I need to fully copy it.
So, if I have an existing object:
MyType someVar = new MyType(someList, false);
Can someone point me in the right direction?
source
share