Java

Java

Made by DeepSource

serialVersionUID should be correctly declared JAVA-E1042

Bug risk
Major
Autofix

The serialVersionUID field must be declared as <access modifier> static final long serialVersionUID. Not declaring it as such will prevent Java from processing it.

The serialVersionUID field has significance when using Java's native serialization API. When declared on a Serializable class, it will be used by Java as a tool to verify proper serialization and deserialization. If it is declared incorrectly, Java will instead opt to generate a serialVersionUID automatically based on the contents of the class.

Such automatic generation can be problematic for certain scenarios. For example, compiling the source with different JDKs may yield different values of serialVersionUID for the same class. If the UID of the serialized data does not match the UID as present in the class loaded in the JVM, an InvalidClassException will be thrown.

Bad Practice

public static int serialVersionUID = 3; // Wrong.

Recommended

Declare serialVersionUID with this specific signature (only the visibility and the value of the field may be changed).

public static final long serialVersionUID = 3L; // Correct.

References