Why not use java clone method

The Java Language is very nice. It has a lot of useful features and APIs. But it isn’t perfect. There are a lot of bad design decisions that, due to the decision of maintaining full backwards compatibility, stays as they were defined virtually forever.

One such a design is related to the clone method. At a first glance, it might seem like a good idea to use a method named clone to copy an object. But as you will see bellow, this is not really the case with Java’s Object.clone method.

Java has an interface called Cloneable. In principle, you should implement this interface if you want to make an object cloneable. The problem here is that this interface doesn’t define any methods. Instead, a clone method is defined in the Object class. Can you spot the mess already? Implementing an interface changes the behavior of a method defined elsewhere.

One more problem. Object.clone is protected, so you must override it with a public method in order for it to be accessible – or you could call reflectively, but then it would get too complicated.

Now why is this so bad? Because an interface should enforce you to implement some behavior in your class. Without implementing the proper method, your code shouldn’t even compile. But all that the Cloneable interface does is to say (in the javadoc documentation) that you should override Object.clone. Also, for the clonning to happen correctly, you would need to have your whole class hierarchy overriding clone. This means all super classes and all mutable objects referenced from all those classes. Are you overriding a 3th party class that doesn’t do so? Too bad.

You can read the documentation of the Cloneable interface here and the one for the Object.clone method here. There is too many strange definitions in there. It tries to enforce things that cannot be done, and doesn’t enforce things that should, among other problems like when you have mutable objects as fields in your cloneable class. Take a look at the book mentioned at the end of the post for more details on this.

So, what should you do? Simple. There is basically two main options you could consider. Having a copy constructor, like:

public Dummy(Dummy dummy) {
    // initialize your fields here
}

Or creating an utility method for copying the object, like:

public static Dummy newInstance(Dummy dummy) {
    // create your object and return it here
}

A lot of the information from this post I learned from the Effective Java book. So please do read it! =)

Advertisements
This entry was posted in java and tagged , , , . Bookmark the permalink.

3 Responses to Why not use java clone method

  1. Fábio says:

    Wow! The best post ever!
    I’ve never imagined how bad is use the Cloneable!

    Each time I hear about this book, I know that I must read it.

    []´s

  2. Pingback: Defensive object copies in java and how scala avoids it « JCranky's Blog!

  3. Pingback: Cópias de defensive objects em Java e como Scala evita isso - iMastersiMasters

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s