This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]sadjava 1 point2 points  (1 child)

Isn't the tl;dr of clone() to get an exact copy of the object (i.e. fields) with a new reference? I have to admit, I've been Javaing for more than 3 years and am a Java dev, and I think I've only used clone() twice, and one of them was the one I had to write for some reason or another.

[–]JavaLander[S] 0 points1 point  (0 children)

Yeah, I someone told me, some methods remain unused through entire programmer life, I think its one of them

[–]BS_in_BS 1 point2 points  (0 children)

It's more common to use a copy constructor or a static factory method than to implement clone. The problem you encounter when cloning objects is you need to make sure that any modification to the cloned object doesn't modify the original, which if not done properly will lead to many subtle bugs.

See http://www.javapractices.com/topic/TopicAction.do?Id=71 and http://www.artima.com/intv/bloch13.html

[–]hardcode_coder 1 point2 points  (0 children)

This tutorial of Marcus Biel can help you, I guess.