Why people are so afraid of using clone() (on collection and JDK classes) ?

Posted by Bozho on Stack Overflow See other posts from Stack Overflow or by Bozho
Published on 2010-04-08T06:24:00Z Indexed on 2010/04/08 6:33 UTC
Read the original article Hit count: 232

Filed under:

A number of times I've argued that using clone() isn't such a bad practice. Yes, I know the arguments. Bloch said it's bad. He indeed did, but he said that implementing clone() is bad. Using clone on the other hand, especially if it is implemented correctly by a trusted library, such as the JDK, is OK.

Just yesterday I had a discussion about an answer of mine that merely suggests that using clone() for ArrayList is OK (and got no upvotes for that reason, I guess).

If we look at the @author of ArrayList, we can see a familiar name - Josh Bloch. So clone() on ArrayList (and other collections) is perfectly fine. (Just look at the implementation).

Same goes for Calendar and perhaps most of the java.lang and java.util classes.

So, give me a reason why not to use clone() with JDK classes?

© Stack Overflow or respective owner

Related posts about java