Monthly Archives: August 2009

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! =)


Import statements in Scala

One more baby step into Scala, in this second of a series of posts. Import statements.

Scala’s import statements are very similar to Java’s, but with some small differences that allow you to write more concise code. Take a look at the following examples:

1. _ instead of *

Java:

import java.util.*;

Scala:

import scala.util._

2. Importing multiple classes from the same package

Java:

import java.util.Date;
import java.util.Collection;
import java.util.List;

Scala:

import java.util.{Date, Collection, List}

3. Renaming

Java:

// can't be done

Scala:

import java.util.{Collection => JavaCollection}

Although I used Java classes in almost all the examples above, you can of course use the same syntax for all the Scala classes as well.

That’s all for now! What do you want to read next about Scala? Leave a comment!


Scala First Impressions

Somewhere in the last few months,  I decided to learn Scala. And master it. There are several reasons to do this, but a couple of them is enough for me:

  • The desire to learn something new, different. Java is nice, I love it. But I want to learn new ways to do things; I want to open my mind to new ideas and possibilities;
  • The urge to be more productive, to be able to spend more time on what matters, and less on boilerplate code. Ruby on Rails is one big inspiration here, but I want something more focused on the Java platform.

So, after discovering about the existence of Scala through one of the Java Posse episodes, this was the language I decided is going to be my next big step in terms of software development.

I still have a LOT to learn. I am just a baby when it comes to Scala. But a few things already hit me. One of those is that Scala source code may seem alien at first. For example, instead of declaring variables like this:

Integer count = 10;

you would do this:

val count: Int = 10

There are a few interesting things about this code:

  • semi-colons are optional;
  • the type definition comes after the variable name, instead of before;
  • the type definition is optional in this case: you know 10 is an integer, right? The Scala compiler knows that as well.

I’ll be posting more about Scala as my learning progresses, so stay tuned! Any expectations? Leave a comment!


Follow

Get every new post delivered to your Inbox.