This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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.
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.