Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Transient addition breaks backward compatibility

807589Jun 26 2008 — edited Jun 30 2008
Hi folks. I have the following problem. I made some changes to a class and I have serialised objects created before the changes. I hoped that the changes I made would still be backwards compatable and that I would still be able to deserialise legacy objects.

I made the following changes...
added
private transient int _hashcode = 0;
and
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    	ois.defaultReadObject();
    	_hashcode = calcHashCode();
}
The changes were motivated by some profiling using JIP which indicated that the hashCode method of my class was being called very repetitively outside of my control, and instead I decided to cache the hashcode instead of recalculating it.

Unfortunately however, now I get a
java.io.StreamCorruptedException
when I try to deserialise a Hashtable containing old objects of this class. Now, I know I can hack a fix into the code; however, I would love if someone could point out my stupidity and tell me why the class is no longer backwards compatable. Is the transient element an issue, or am I doing something stupid with the readObject method? The calcHashCode() method simply reinitialises the _hashcode field by calculating it's values from other fields.

Thanks.

B.T.W. I use
public static final long serialVersionUID = 1l;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 28 2008
Added on Jun 26 2008
3 comments
116 views