Monthly Archives: September 2009

Implementing Java Serializable is not as simple as it seems

Sometimes we have to send objects “over the wire” to remote machines. Or we have to write them on disk. Or for any other unknown reason, you have to make your entity class Serializable. Maybe you are using an API that asks for it and you don’t know, nor care, why.

So it is simple, right? Just add implements Serializable to your class and you are done. Well… not really. The problem is that a lot of developers think this way, just because of a lack of knowing better. So this post is here to save you!

Lets start understanding what implementing Serializable means. When you write something like

public class MyClass implements Serializable {
    private String myString;
}

you are basically saying that your entity has one field, myString, which is part of the logical representation of the class. So the compiler will assume it can use this information to, say, serialize (write) the object to disk, and do the opposite when deserializing it.

So far so good. Now suppose you sent this class to a friend. After a while, you evolve your class and add an utility method, but your friend still uses the old version. Well, now the serialized forms of your local class and your friends’ are incompatible. If you try to deserialize a class your friend serialized, you will get an InvalidClassException.

Fortunately, the solution to this problem is easy: declare a serialVersionUID, like this:

public class MyClass implements Serializable {
    private static final long serialVersionUID = 42L;
    private String myString;
}

If you do this from the start, both your object and your friends’ will be compatible! Some IDEs even do this automatically for you =)

Now, you may wonder why that happened, if all you did was to add an utility method, something in principle unrelated to the logical representation of the class? What happens is that the default algorithm for generating a serialVersionUID takes all kinds of class members into account, which includes our utility friend.

This is all for now. But there is more to this topic. Next, we will look into more complex problems related to object serialization. Get ready!


Globalcode’s Casual Class: JAVAEE 6

This is a little late, but anyway…

Two weeks ago I attend one more of the Globalcode‘s Casual Class‘es. This one was about JAVAEE 6 and the main technologies that will be part of it when it comes out – apparently very soon.

I don’t have many pictures because I’m very smart and let my mobile’s battery run out… but let’s go through a few of the ones I got and talk about the contents of this event.

Preparations

Preparations

The event was split in a few individual topics, among them: JSF 2.0, JAX-RS, EJB 3.1 and JPA 2.0. Each one had different presenters, all of them great speakers! =)

JSF 2.0

JSF 2.0

JSF 2.0 was presented by Yara Senger and Alberto “Spock” Lemos. They mentioned some changes that are coming to JSF, and used a small demo application, SCRUM Toys, to explain how they got involved into actually using the new features.

JPA 2.0

JPA 2.0

Melissa Vilela spoke about the new features coming in JPA 2.0. There are a lot of nice things. Among them, the new Criteria API is not. To be brutally honest, that thing is ridiculously complex. It is clearly lacking some usage of the KISS principle.

Then there was a presentation about REST, more specifically JAX-RS. (no pictures, sorry). Nothing really new here, just the fact that the API, which was already done and available, is going to be part of JAVAEE 6.

EJB 3.1

EJB 3.1

The last topic was EJB 3.1. Like with EJB 3.0, things are moving towards a simpler development model, and this is what was presented. Nice, simplifications to EJBs are always welcome! =)

After that, Pizza! uhu! My only complain this time is that the technical sessions took longer than usual, and I was really hungry in the end! But it was well worth the time =)


Follow

Get every new post delivered to your Inbox.