title: Updating one list also updating referenced list in Java
body: <p>I am facing strange problem (At least awkward for me). I have a list of custom objects. I am adding this list of custom objects 2 other ArrayLists. Here is the problem, when I update one of the list (a property of any custom object), it updates the object of same location in other list. Below is the code</p>
  <p>Below is the custom class for example:</p>
  <pre><code>public class TestClass {
      String name;
  }
  </code></pre>
  <p>Here is how I am creating data set:</p>
  <pre><code>                    TestClass testClass1 = new TestClass();
                      testClass1.name= &quot;first&quot;;
  
                      TestClass testClass2 = new TestClass();
                      testClass2.name= &quot;second&quot;;
  
                      List&lt;TestClass&gt; data = new ArrayList&lt;&gt;();
                      data.add(testClass1);
                      data.add(testClass2);
  </code></pre>
  <p>Here is how I am adding data set in other 2 Lists:</p>
  <pre><code>                    List&lt;TestClass&gt; testListFirst = new ArrayList&lt;&gt;();
                      testListFirst.addAll(data);
  
                      List&lt;TestClass&gt; testListSecond = new ArrayList&lt;&gt;();
                      testListSecond.addAll(data);
  </code></pre>
  <p>Here is the problem when I update an element of one list it gets updated in second list as well:</p>
  <pre><code>testListFirst.get(0).name = &quot;third&quot;;
  </code></pre>
  <p>If I check <strong>testListFirst</strong> it is updated with new value, but <strong>testListSecond</strong> is also updated. My expectation was <strong>testListSecond</strong>   It should not get updated because they both list are different object in memory pointing different objects. If I update one other should not be updated. Please correct me if I am wrong. Any help is highly appreciated.</p>