views:

408

answers:

3

Hi All, 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?

+4  A: 

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.

Robert Munteanu
Thanks for the quick response! I was just able to use the serialver tool to verify this. Before: serialVersionUID = 7949745139928005557L. After: serialVersionUID = 7699267648627402486L.
Pete Doyle
The UID may also differ depending on which Java compiler is used.
Esko Luontola
Of course, if you choose the right method name, you can get back the same UID.
Tom Hawtin - tackline
@Tom Hawtin - tackline : care to provide an example?
Robert Munteanu
+1  A: 

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.

boxofrats
I believe Eclipse can generate this warning for all classes, not just Serializable ones
matt b
A: 

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.

Ravi Wallau