Question

If I have a class like this:

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;
    }
}

Will its calculated serialVersionUID change if I add another method (and no additional fields)?

For example, adding the method:

public String getFullName() {
    return firstName + " " + lastName;
}

Alternatively, is there a nice tool for figuring this out?

Was it helpful?

Solution

Yes, it will change the serialVersionUID.

You can use the serialver command line tool - at least in Sun's JDK to calculate the serialVersionUID for a given compiled class.

OTHER TIPS

Yes, it will change. An easy way is Eclipse: If the class implements Serializable and you do not provide a serialVersionUID field, the IDE displays a warning symbol. You can click on the warning sign and select "Add generated serial version UID". This will calculate a serialVersionUID for you and insert it in your code.

When I started testing my beans it seemed to me that the serialVersionUID was regenerated when the class was compiled, not only when its signature changed. The reason for that could be the maven build and the fact that the old class was deleted before the new build.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top