I’m serializing an object to a blob in a SQLiteOpenHelper like so:
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(value);
out.close();
Each class define its own serialVersionUID that never change.
How does the serialization/deserialization actually work? Does it remember the name of the field, and the value associated with it? If I keep adding/removing field, is there a chance that an existing field have their value modified?
For example, let’s say I serialize this class:
public class Foo implements Serializable {
private static final long serialVersionUID = 4068188167994381987L;
String x;
int y;
}
Where x is "A", and y is 1;
Then I modify the class like so:
public class Foo implements Serializable {
private static final long serialVersionUID = 4068188167994381987L;
float f;
String x;
}
If I keep adding/removing field, will each time that I deserialize Foo x still be “A”?
You cannot delete a field. See here at Oracle for compatible changes to your
Serializableclass.Yes after the change in class
Fooa deserialization of a olderFoowould set x to ‘A’, but because you removed a field this does not work.I suggest keeping the old field
yand marking it@deprecated: