I have an array of objects that I am able to read and write to disk. But now I want to add a member variable to my class. If I add the member variable, I'll get a class incompatible error and I won't be able to read my serialized objects. Is there a way to modify the member variables of a class used for serialization without losing the ability to read persisted objects...or an easy way to convert the persisted objects to the new class?
This post from SO that should answer your question. It even explains how to find the serial version automatically created by compiler if you didn't specify it (find serialver
in referenced page). Old articles can still be interesting ...
In short for the impatient :
serialver
tool of the JDK can give you the serial version automatically generated by the compiler if you did not specify itSo you just have to find this serialversion, add the magic field static final long serialVersionUID = xx
and as you are only adding a member variable all should be fine.
(The referenced post is first answer from google for java serialization multiple versions
)