Java - cloning a property inside getter method

People,

I discussed the best Java coding techniques mentioned here
http://viralpatel.net/blogs/most-useful-java-best-practice-quotes-java-developers/

The second quote says

Quote 2: Never create instance fields of class public

I agree that is absolutely correct, but I am stuck with the author’s recommendation a few lines below this quote.

He says:


private String[] weekdays = {"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"}; public String[] getWeekdays() { return weekdays; } 

But the getter method does not exactly solve our problem. The array is still available. The best way to make it unmodified is to return a clone of the array instead of the array itself. Thus, the getter method will be changed to

 public String[] getWeekdays() { return weekdays.clone(); } 

I have never used clone() inside any getter method of a Java class.

I am wondering ( as mentioned, as one of the good practices ) - why should use one should use / shouldn't use clone() inside the getter method? and in what scenarios?

Is this consistent with good coding practice for Java?

thanks

+7
source share
4 answers

This is discussed in the book Effective Java by Joshua Bloch. There is a section called “Make protective copies if necessary” (section 39 in the 2nd edition).

I think Google books may allow you to preview the section preview.

A good book to discuss such topics.

+5
source
 private String[] weekdays = {"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"}; public String[] getWeekdays() { return weekdays; } 

If you do not use the clone method, a user of this class can do many unethical things:

  • change the order of days
  • change the name of the day
  • ...

But returning the clone will not affect the class and data. This way, other users of the class will not be affected.

+3
source

You clone() or System.arraycopy() on get() if you want to ensure that the entire graphic object (containing the array) is immutable. This is usually done when the API that provides the array is publicly available and there are restrictions on the values ​​in the array or when multiple threads access objects. In such cases, immutability is important.

Say you have a GroceryStore object that has a getItemsSortedByPrice() method. You store elements in an array that maintain order by price, but if you return this array, client code can change it and break the (internal) invariants of your object.

If this is internal code (i.e.) that is not part of the public API, and you know that you will not modify the array, then cloning / copying is probably not necessary, as this is detrimental to performance without real benefit.

It all depends on the context.

Arrays are just objects, and all (im) rules / variation methods applicable to ordinary objects also apply to arrays.

+2
source

I think your usecase does not match the suggested Java code. For example, for a different usecase than yours.

The last array sounds to me like Enums , and I think it fits your requirement a lot better.

+1
source

All Articles