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 have an object (that implements Serializable-interface) written to a file some days ago. no problems occured.
today i wanted to read the object back from the file, but the object that's in the file contains some other members of other classes (all implementing Serializable interface) and some of this classes are changed (new members, methods, etc.)
when i try to readObject from file, i got this error:
INFO: Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true java.io.InvalidClassException: be.aquafin.webgis.kaart.ComplexKaart; local class incompatible: stream classdesc serialVersionUID = -1104219041991736069, local class serialVersionUID = 150420720706126891
so if i save object to file, change some classes, try to get file back, i will always get error. or could i prevent this from happening ?
You could add a serialVersionUID to all your Serializable classes. If you haven't defined this then the JVM will try to derive a value for it based on the methods and fields of the Serializable class. So if you've changed/added methods the derived serialVersionUID will be different, but if you define it yourself it will remain unchanged.
Of course, once you explicitly define serialVersionUID, you also have to take on versioning: Is the new version of your class compatible with the old, as far as serialization is concerned? [ October 19, 2005: Message edited by: Jeff Albrechtsen ]