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: x.clone() != x x.clone().getClass() == x.getClass() x.clone().equals(x) Note that condition (1) must always be satisfied in all the cases. Though conditions (2) and (3) are not absolute requirements, it is good to design clone method in such a way that these hold good. Before going ahead with discussion, here is the method signature of clone method in Object class: protected native Object clone() throws CloneNotSupportedException; So
1 Comment
Auth Gábor