[ACCEPTED]-What does the "declare a static final serialVersionUID" warning mean and how to fix?-java

Accepted answer
Score: 30

This is explained fairly well here:

The serialVersionUID is a universal version identifier for a Serializable class. Deserialization uses this number to ensure that a loaded class corresponds exactly to a serialized object. If no match is found, then an InvalidClassException is thrown.

You fix 7 the error by adding

private static final long serialVersionUID = 7526472295622776147L;  // unique id

to the class.

Further reading:


A side note: If you're 6 using Eclipse and if you (and no one else) ever 5 plan to serialize your classes, you can 4 also suppress the error by going to

     Window 3 → Preferences → Java → Compiler → Errors/Warnings 2

and select "Ignore" on "Serializable 1 Class without serialVersionUID".

Score: 6

just add

private static final long serialVersionUID = 1L;//or some long

Docs describe it pretty well

The serialization 36 runtime associates with each serializable 35 class a version number, called a serialVersionUID, which 34 is used during deserialization to verify 33 that the sender and receiver of a serialized 32 object have loaded classes for that object 31 that are compatible with respect to serialization. If 30 the receiver has loaded a class for the 29 object that has a different serialVersionUID 28 than that of the corresponding sender's 27 class, then deserialization will result 26 in an InvalidClassException. A serializable class 25 can declare its own serialVersionUID explicitly 24 by declaring a field named "serialVersionUID" that 23 must be static, final, and of type long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

If 22 a serializable class does not explicitly declare 21 a serialVersionUID, then the serialization 20 runtime will calculate a default serialVersionUID 19 value for that class based on various 18 aspects of the class, as described in 17 the Java(TM) Object Serialization Specification. However, it 16 is strongly recommended that all serializable classes 15 explicitly declare serialVersionUID values, since 14 the default serialVersionUID computation is 13 highly sensitive to class details that 12 may vary depending on compiler implementations, and 11 can thus result in unexpected InvalidClassExceptions during 10 deserialization. Therefore, to guarantee 9 a consistent serialVersionUID value across different 8 java compiler implementations, a serializable 7 class must declare an explicit serialVersionUID 6 value. It is also strongly advised that 5 explicit serialVersionUID declarations 4 use the private modifier where possible, since such 3 declarations apply only to the immediately 2 declaring class--serialVersionUID fields 1 are not useful as inherited members.

Also See

Score: 1

Declare it like that:

private static final long serialVersionUID = -4673040337179571462L;

Its required by serializable 1 interface.

Score: 0

Are you extending a Swing object?

I often 6 use the @suppressWarnings annotation to 5 rid myself of this warning.

I guess the question 4 of surpressing the warning or supplying 3 a unique ID depends on the scale of your 2 application and your intended use of Object 1 serialisation.

More Related questions