How can I create a copy of my data type I created in Java?
<p>If I have a class:</p>

<pre><code>public class MyType  
{  
    private List&lt;Integer&gt; data;  
    private boolean someFlag;

    public MyType(List&lt;Integer&gt; myData, boolean myFlag)
    {
        this.data = myData;
        this.myFlag = someFlag; 
    } 
}
</code></pre>

<p>Now, if I create an instance of MyType, how do I do a deep copy of it?  I don't want the new object to point to the old reference, but an entirely new instance.</p>

<p>Is this a case when I should implement the Cloneable interface, or is that a used for shallow copies? </p>

<p>I can't just do:</p>

<pre><code>MyType instance1 = new MyType(someData, false);  
MyType instance2 = new MyType(instance1.getData(), instance1.getFlag());
</code></pre>

<p>I'm concerned about new instances of MyType pointing to the same reference for its "data" variable.  So I need to copy it entirely.</p>

<p>So, if I have an existing object:</p>

<pre><code>MyType someVar = new MyType(someList, false);

// Now, I want a copy of someVar, not another variable pointing to the same reference.
</code></pre>

<p>Can someone point me in the right direction?</p>
