javasonarqubeserializablecorrectnessserialversionuid

Sonarqube error: Class defines a computed serialVersionUID that doesn't equate to the calculated value


Correctness - Class defines a computed serialVersionUID that doesn't equate to the calculated value

This serializable class defines a serialVersionUID that appears to be a computed value, however the value does not match the computed value, and thus losses it's value as version indicator. Either create a custom value like 1, 2, 3, 4.. etc, or recompute the serialVersionUID using your IDE.

Probably referring to the field:

private static final long serialVersionUID = 18234907734L;

Why 18234907734L isn't valid?


Solution

  • 18234907734L is not valid because you made a change that makes the serialized form of the new version of the class incompatible with the serialized form of the old version. You were supposed to update the value of serialVersionUID but sonarqube detected that you didn't.

    (Sonar probably does not compare the old and the new version numbers, instead it knows what algorithm IDEs use to generate the version number, and it checks if the id matches its the calculation)

    Change

    private static final long serialVersionUID = 18234907734L;
    

    To

    private static final long serialVersionUID = 1L;
    

    And don't forget to update it when you change the class structure in some way, like by adding a new field or removing a field.

    See also What is a serialVersionUID and why should I use it?