Error while deserializing
1 posts in topic
Flat View  Flat View

Posted By:   Rohit_Seth
Posted On:   Tuesday, September 18, 2001 11:51 PM


I am able to serialize an object using ObjectOutputStream.writeObject

However, when I try to deserialize and read using ObjectInputStream.readObject, I am getting following error:- Class Not Compatible

Why this should happen?

Re: Error while deserializing

Posted By:   Andrew_Watson  
Posted On:   Wednesday, September 19, 2001 02:00 AM

Hi Rohit
Hi Rohit

There are three possible scenarios on how to upset the default java serialisation process. I think that scenario 1 is the one for youÂ…

Scenario 1

Incompatible change so Java's default de-serialisation mechanism cannot read the value of the instance variables anymore (e.g. changing the type of a primitive instance variable from int to long). Java detects that the hash code of the new version of the class is different from the hash code in the serialised object.

Working Class Failing Class
private int value;

public int getValue() {

return value;

private long value;

public int getValue() {

return (int)value;


Scenario 2

Making an incompatible change, but pretending to be compatible with the old version.
Java's default de-serialisation mechanism detects that, even though the serial version UID of the new version of the class is the same as the one of the serialised object, the serialized object is not structurally compatible with the new version of the class. The serialised object actually also contains type descriptors for all instance variables.

Scenario 3

Making an incompatible change, pretending to be compatible with the old version, which actually works. The name of an instance variable can be changed in a new version, but that this does not lead to any exception being thrown. Also that now the new version has a new instance variable, which did not exist in the old version, leads to no exception. The default de-serialization mechanism leaves the value of the previous variable just untouched.
About | Sitemap | Contact