File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes When to use serialVersionUID Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "When to use serialVersionUID" Watch "When to use serialVersionUID" New topic

When to use serialVersionUID

Jimmy Ho
Ranch Hand

Joined: Jul 31, 2007
Posts: 61
So I'm doing some object serialization that will be passed between different JVMs on our networks here. I've been looking around, and I can't find basic information on what I'm supposed to add the serialVersionUID to.

Obviously, a class that implements Serializable that's going to be serialized is a candidate for adding an explicit serialVersionUID.

But what if I have the following structure? (from a Simple Factory Pattern implementation):
* interace A extends Serializable
* abstract class B implements A
* class C extends B

I know I should add a serialVersionUID to C, but what about interface A and abstract class B?
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Hi Jimmy,

You don't need to add the serialVersionUID to the interface, but you should to both classes. According to the Java API documentions, "it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization." Another (not foolproof way, but useful) is to use an IDE like Eclipse and enable warnings for serializable classes that don't declare the serialVersionUID. It will tell you when you are missing it.

I agree. Here's the link:
subject: When to use serialVersionUID
It's not a secret anymore!