We help IT Professionals succeed at work.

Java object Serializable new Properties

va3jsl
va3jsl asked
on
Is there any way to add properties to a class that is saved using:

ObjectOutputStream oos = new ObjectOutputStream(.....));
oos.writeObject(myObject);

I have a number of objects already saved and would like a way to retrieve them into the new version of my object that has additional properties (obviously these will be null until the new version is saved)
Thanks
Comment
Watch Question

You have to provide serialVersionUID field manually and ensure it is always the same no matter what changes are made to the file.

http://java.sun.com/developer/technicalArticles/Programming/serialization/

Look for a detailed description of version control in the above article.

Hope this helps.
CERTIFIED EXPERT
Commented:
If the serialVersionUID differs (which means the class version has changed) , you will get a incompatible class error while trying to deserialize
Yes

private static final long serialVersionUID = 12121212L;

put this in the class and the serial version remains same even if you add new fields.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.