How to insure that serialVersionUID changes when the class does?
Joined: Sep 12, 2003
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.
Joined: Aug 24, 2005
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
Joined: Apr 21, 2006
safest would be to write custom deserialisation code that detects missing field values and handles them appropriately.
Joined: Dec 31, 2004
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?
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.