Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is method name considered or generating the hash for serialversionuid

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;
}
like image 990
user1678270 Avatar asked Nov 26 '25 04:11

user1678270


1 Answers

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.

like image 186
mrks Avatar answered Nov 28 '25 18:11

mrks