I have a serialized class that has variables ABC. I declare a serialVersionUID in the class. I save an instance of the class into a text file using a vector. If I were to change the class (add variable D) I now have 2 different classes (the ABC that I am trying to read from the file, and the ABCD that is in the file). If I keep the serialVersionUID the same, will it just ommit D sense what I am trying to read only has ABC? example:
Class MyClass implements serializable
{
serialVersionUID = 12345;
int a = 0;
int b = 0;
int c = 0;
}
Class implmentation
{
MyClass me = new MyClass();
me.a = 2;
me.b = 4;
me.c = 6;
}
// save to txt file using objectOutputStream
// later change myClass to add int d = 0;
if I wanted to read the origional myClass object (me) from the text file, will it still be able to read it?
Sorry i don’t have a SSCCE but i want to verify before i start working on this.
please take a look here http://denis-zhdanov.blogspot.com/2009/09/serialversionuid-update-policy-on-class.html
deleting fields – If a field is deleted in a class, the stream written will not contain its value. When the stream is read by an earlier class, the value of the field will be set to the default value because no value is available in the stream. However, this default value may adversely impair the ability of the earlier version to fulfill its contract;
adding fields – When the class being reconstituted has a field that does not occur in the stream, that field in the object will be initialized to the default value for its type. If class-specific initialization is needed, the class may provide a readObject method that can initialize the field to nondefault values;