You can implement the private readObject (ObjectInputStream in) object in your object class. It kind of "overrides" the default behavior, although it is declared as "private", so technically it should not. Basically, when a Java serializer needs to read an object from a stream, a class that implements this method, it will call this instead of performing its default task.
So, you can implement logic in it that will read all existing fields from the stream and assign default values ββto those that are missing.
Edit: As @EJP points out (thanks, @EJP!), This does not quite work. You must also define the private static long serialVersionUID in your class and set it to the "old" value that you see in the exception.
Also, consider replacing Serializable with Externalizable for the future, this will give you additional flexibility and transparency. The Externalizable extension tells java that you intend to handle serialization yourself, and then it will not try to complete its default task, which throws an exception.
In this case, implement readExternal(ObjectInputStream in) to read the members from the stream one by one and initialize those that are not available for any default value.
source share