title: Sonar - Store a copy - Mutable members should not be stored or returned directly
body: <p>I have a list which is a private member in my class.
  I have used getter and setter to get and set the values.
  SOnar throws an error - Mutable members should not be stored or returned directly.</p>
  
  <p>For example: ABC and DEF are two classes.</p>
  
  <pre><code>class ABC{
  private List&lt;DEF&gt; defList;
  public List&lt;DEF&gt; getDefList() { return defList; }
  public void setDefList(List&lt;DEF&gt; defList) { this.defList = defList; }
  </code></pre>
  
  <p>After lot of googling and searching, I have understood that the getter can be changed as follows:</p>
  
  <pre><code>public List&lt;DEF&gt; getDefList() { return new ArrayList&lt;&gt;(defList); }
  </code></pre>
  
  <p>When i try to use setter similarly, </p>
  
  <pre><code>public void setDefList(List&lt;DEF&gt; defList) { this.defList.addAll(defList); }
  </code></pre>
  
  <p>then the variable starts showing </p>
  
  <pre><code>'private field 'defList' is never assigned.
  </code></pre>
  
  <p>May I know the correct way to do when it is a list, (a list of another class)</p>
  
  <p>Note: Both the answers from Prasad Karunagoda and Leo Aso  works. I cant mark both as accepted answer. So having a note here</p>