Suppose I serialize the below class (by putting a value to field x, say 5), and before doing the deserialization, I remove field x and put a new field y (int or float), what will be the result?
I believe deserialization is done successfully and value of field y will be default, is this correct?
If this is correct, where will the value if x will go in the transition from persistent object to stateful object at serialization time?
public class Cat implements Serializable{
private static final long serialVersionUID = 4231235177539824282L;
int x;
}
before deserialization:
public class Cat implements Serializable{
private static final long serialVersionUID = 4231235177539824282L;
float/int y;
}
The serialization spec details the inner workings of field deserialization (you can find it here: http://docs.oracle.com/javase/7/docs/platform/serialization/spec/input.html)
If you browse through the “readObject method” section it states:
So in essence, the field ‘x’ will have some value in the stream, but will be discarded and the field ‘y’ will be set to 0 (default value) since it does not appear in the stream.