title: How does object reference and cloning works in java
body: <p>Below is the code</p>
  
  <pre><code>ArrayList arList = someMethod();// returning ArrayList with customDO objects
  </code></pre>
  
  <p>Now somewhere in different class I am getting data from this arList</p>
  
  <pre><code>CustomDo custDO= (CustomDO)arList.get(0);
  </code></pre>
  
  <p>Will the arList be alive as long as custDO is alive ? If yes, will below piece of code help</p>
  
  <pre><code>CustomDO custDO = ((CustomDO)arList.get(0)).cloneMe();
  // where cloneMe has defintion as return ((CustomDO)super.clone());
  // CustomDo implements Cloneable
  </code></pre>
  
  <p>Is there a better way to keep a copy of only the first element of arList and discard the list so that it can be collected by garbage collector ?</p>