title: Accessing private variable of Model class without getters
body: <p>I have a Model class defined in my project. and as usual it has some private variables and public getters and setters</p>
  
  <pre><code>public class Person{
  
   private ArrayList&lt;String&gt; mark;
  
   public void setMark(ArrayList&lt;String&gt; mark){
    this.mark = mark;
   }
  
   public void getMark(){
    return this.mark;
   }
  }
  </code></pre>
  
  <p>Suppose in some other class I am using this Model like</p>
  
  <pre><code>Person person = new Person();
  ArrayList&lt;String&gt; mark = new ArrayList();
  mark.add("10");
  mark.add("15");
  mark.add("18");
  person.setMark();
  </code></pre>
  
  <p>then the private variable of person holds the value "my name", the I am accessing the variable using public getter of the class like</p>
  
  <pre><code>ArrayList&lt;String&gt; localMark = person.getMark()
  </code></pre>
  
  <p>so as per my knowledge person.getMark() returns the reference of private variable name, so if I modify the local variable 'localMark', then it will effect the private variable of Person class, so there it breaks the private property of the variable</p>
  
  <p>ex: </p>
  
  <pre><code> ArrayList&lt;String&gt; localMark = person.getMark();
   System.out.println(localMark.get(0)); // will be "10"
   localMark.set(0,"25") // person.mark will be changed
   System.out.println(person.getMark().get(0)); //will be printing "25"
  </code></pre>
  
  <p>most of the developers following the same design pattern I guess, but what is the correct way to create <code>Models</code></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As per the comment of vinod I checked, and Strings it passes value but not reference but for ArrayList... it returns reference.</p>