title: Java - Clone the property inside getter method
body: <p>Folks,</p>
  
  <p>I was going through Java's best coding practises mentioned here<br>
  <a href="http://viralpatel.net/blogs/most-useful-java-best-practice-quotes-java-developers/" rel="noreferrer">http://viralpatel.net/blogs/most-useful-java-best-practice-quotes-java-developers/</a></p>
  
  <p>2nd quote says,</p>
  
  <blockquote>
    <p>Quote 2: Never make an instance fields of class public</p>
  </blockquote>
  
  <p>I agree that's absolutely correct, but I got stuck for following writer's recommendation few lines below this quote.</p>
  
  <p>He says,</p>
  
  <hr>
  
  <pre><code>private String[] weekdays = 
      {"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"};
  
  public String[] getWeekdays() {
      return weekdays;
  }
  </code></pre>
  
  <p>But writing getter method does not exactly solve our problem. The array is still accessible. Best way to make it unmodifiable is to return a clone of array instead of array itself. Thus the getter method will be changed to</p>
  
  <pre><code>public String[] getWeekdays() {
      return weekdays.clone();
  }
  </code></pre>
  
  <hr>
  
  <p>I have never myself used <code>clone()</code> inside any getter method of Java class. </p>
  
  <p>I am wondering ( <strong><em>as it is mentioned to be one of the good practises</em></strong> ) - why one <code>should use</code> / <code>shouldn't use</code> <code>clone()</code> inside getter method ? and in which scenarios ?</p>
  
  <p>Does it qualify to be a good coding practise for Java ? </p>
  
  <p>Thanks</p>