aspose file tools*
The moose likes Java in General and the fly likes How to insure that serialVersionUID changes when the class does? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to insure that serialVersionUID changes when the class does?" Watch "How to insure that serialVersionUID changes when the class does?" New topic
Author

How to insure that serialVersionUID changes when the class does?

James Adams
Ranch Hand

Joined: Sep 12, 2003
Posts: 188
I usually add an Eclipse-generated serialVersionUID to all classes which implement Serializable. However this value stays the same after I modify the class, which breaks the reliability of the deserialization which uses this variable. Is there a way to insure that this variable gets updated every time there's a significant change to the class -- preferably something automatic through Eclipse which doesn't rely on remembering to do this by hand?

Thanks in advance for any suggestions.


--James
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
If you want the number to change automatically, do not set it yourself and javac will choose a random one.

If you want it to always be the same except when you make a change that breaks compatibility you'll need to update the UID yourself manually. I'm not aware of any existing tool that will automatically detect those kinds of changes.

I suppose you could write a task for your build process or something like a CVS check-in script that detects certain changes and updated the UID.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
safest would be to write custom deserialisation code that detects missing field values and handles them appropriately.


42
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961

I'm not aware of any existing tool that will automatically detect those kinds of changes.


What about the serialver tool distributed within the bin directory of the JDK?

See SerialVer documentation for Windows / Linux and Solaris
[ December 19, 2006: Message edited by: Edwin Dalorzo ]
James Adams
Ranch Hand

Joined: Sep 12, 2003
Posts: 188
After doing a bit of research into this topic it seems that adding your own serialVersionUID variable to a Serializable class is a bad a idea.

As an example here is an article from IBM which a colleague pointed out to me in which the author advocates against using serialVersionUIDs:

http://www-03.ibm.com/developerworks/blogs/page/woolf?entry=serialization_and_serial_version_uid

From the article:

So should programmers specify serialVersionUID in their serializable classes? I think not. It just leads to trouble. If you really understand what you're doing with serialization, and you're convinced that two class versions are truly compatible for serialization and deserialization purposes even though their code is different, and if you've tested this well to validate your assumptions, then you can use serialVersionUID to override the deserializer's assumptions and specify that the two versions are compatible. But otherwise, the safe move is that whenever you change a class' code, you should change its serialVersionUID value. But since this is how Java works by default anyway, just don't bother with serialVersionUID and let Java take care of things for you.



--James
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to insure that serialVersionUID changes when the class does?