I studied Java is pass object reference by value, and in order to make a local copy of an object, I can either do clone() or copy-constructor. I also looked at deep/shallow
Passing the list into the constructor is probably the best way to go. The constructor invocation itself will use, behind the scenes, System.arraycopy. So it will effectively detach the local copy from the list passed in through the constructor.
Use the copy constructor whenever you can. clone()
is obsolete and should not be used (neither implemented) in new code.
If you extend a class that implements
Cloneable
, you have little choice but to implement a well-behavedclone
method. Otherwise, you are better off providing an alternative means of object copying, or simply not providing the capability. [...] A fine approach to object copying is to provide a copy constructor or copy factory. [...]Given all of the problems associated with
Cloneable
, it’s safe to say that other interfaces should not extend it, and that classes designed for inheritance (Item 17) should not implement it. Because of its many shortcomings, some expert programmers simply choose never to override theclone
method and never to invoke it except, perhaps, to copy arrays.
From Effective Java 2nd Edition, Item 11.
It seems by question that you want to make your collection unmodified so you can use Collections.unmodifiableList()
.