Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding of FindBugs warning about serialVersionUID field

I have the following class signature and ClientEventSourc implements Serializable:

public class Grid extends ClientEventSource implements Focusable, FramingBlockWrapper,LIMSEditableField

Now FindBugs is listing this as dodgy :

Class is Serializable, but doesn't define serialVersionUID

This class implements the Serializable interface, but does not define a serialVersionUID field.  A change as simple as adding a reference to a .class object will add synthetic fields to the class, which will unfortunately change the implicit serialVersionUID (e.g., adding a reference to String.class will generate a static field class$java$lang$String). Also, different source code to bytecode compilers may use different naming conventions for synthetic variables generated for references to class objects or inner classes. To ensure interoperability of Serializable across versions, consider adding an explicit serialVersionUID.

Can some explain what it means and what is the best possible way to fix this ?

like image 740
Geek Avatar asked Aug 09 '12 10:08

Geek


1 Answers

the serialVersionUID is used to deserialize your class. it is auto-generated by default.
but changing anything in your class would generate a different serialVersionUID and you cannot deserialise "old" objects.
so you define your own serialVersionUID to help deserialisation find the right class.

add a variable like this to your code:

private static final long serialVersionUID = 6106269076155338045L;

for reference on generating your UID:

Does it matter what I choose for serialVersionUID when extending Serializable classes in Java?

like image 139
reggaemuffin Avatar answered Sep 27 '22 19:09

reggaemuffin