I have a class while serialization
public class Name implements Serializable {
private final String firstName;
private final String lastName;
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
But while de-serializing I have an extra method(mentioned below) that is not affecting object state in any way and serialization is all about storing the object state then why an extra method is having contribution in the hash generation for serialversionuid. In current scenario it will fail with InvalidClassException. But object state is not getting changed by this extra method.
public String getFullName() {
return firstName + " " + lastName;
}
The default is to make sure that your serialized objects are only compatible if they come from exactly the same class. For this, a number of attributes of the class are taken into account: https://docs.oracle.com/javase/6/docs/platform/serialization/spec/class.html#4100
In any case, if you use serialization, you should define your own id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With