Clone() has a series of pitfalls like pointed out in "Effective Java" by Joshua Bloch, so a copy constructor should be used instead.
In Java there isn't a default copy constructor like in C++, so it has to be created.
Copying requires more processing than passing a reference, however it's very useful for returning defensive copies of internal objects in accessors and reduce mutability.
Here's an example of copy constructor:
class Example { private int value; public Example(Example example) { this.value = example.value; } }
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.