3/14/2007

serialVersionUID

static final long serialVersionUID = -4544769666886838818L;

What does this mean to Gosling's tool..?

It's used when deSerializing an object, to determine whether that object is compatible with that object's class file in the JVM doing the deserialization. If the serialVersionUID of the class file doesn't match the serialVersionUID of the deserialized object, you'll get an InvalidClassException. If the class file doesn't explicitly declare a serialVersionUID, then the serialization runtime has to compute it, which is a relatively expensive process.

So what makes the Object that was serialized and its class incompatible?
   1. Deleting fields from the class
   2. Changing the type of a field
   3. Changing a class from Serializable to Externalizable or vice versa


Keep the Pandoras box closed by adding a serialVersionUID to the class file.

2 comments:

karthik said...

Good one.
Also, when i had a glance on InvalidClassException and its causes, found these in suns doc.

* The serial version of the class does not match that of the class descriptor read from the stream
* The class contains unknown datatypes.
* The class does not have an accessible no-arg constructor.

I am confused a bit on the last cause. If the class doesnt have an accessible constructor i believe the JVM would create a no-arg cons.

Any other-behaviour when it comes to serialization?..
Thoughts?.

Dinesh said...

Sorry for the delayed post....
The argument holds good when the class doesn't have the constructors overloaded!

dzone.com