Which is better option: Cloning or Copy constructors?

by Prasanth Gullapalli

Here is how I started writing this article. I have read this statement so many times: “Cloning becomes difficult when the object has references to mutable final fields.” And every time I google about it, understand what exactly this means and as part of the process forget about it too. So thought I would blog this so that this will serve as my immediate reference.

Cloning an object, what I could recall from my OOP course in my graduate studies, is creating a similar copy of an object which basically should conform to the following rules:

(1) x.clone() != x
(2) x.clone().getClass() == x.getClass()
(3) x.clone().equals(x) Continue reading