Eclipse issues warnings when a serialVersionUID
is missing.
The serializable class Foo does not declare a static final serialVersionUID field of type long
What is serialVersionUID
and why is it important? Please show an example where missing serialVersionUID
will cause a problem.
If you get this warning on a class you don't ever think about serializing, and that you didn't declare yourself
implements Serializable
, it is often because you inherited from a superclass, which implements Serializable. Often then it would be better to delegate to such a object instead of using inheritance.So, instead of
do
and in the relevant methods call
myList.foo()
instead ofthis.foo()
(orsuper.foo()
). (This does not fit in all cases, but still quite often.)I often see people extending JFrame or such, when they really only need to delegate to this. (This also helps for auto-completing in a IDE, since JFrame has hundreds of methods, which you don't need when you want to call your custom ones on your class.)
One case where the warning (or the serialVersionUID) is unavoidable is when you extend from AbstractAction, normally in a anonymous class, only adding the actionPerformed-method. I think there shouldn't be a warning in this case (since you normally can't reliable serialize and deserialize such anonymous classes anyway accross different versions of your class), but I'm not sure how the compiler could recognize this.
Why use
SerialVersionUID
insideSerializable
class in Java?During
serialization
, Java runtime creates a version number for a class, so that it can de-serialize it later. This version number is known asSerialVersionUID
in Java.SerialVersionUID
is used to version serialized data. You can only de-serialize a class if it'sSerialVersionUID
matches with the serialized instance. When we don't declareSerialVersionUID
in our class, Java runtime generates it for us but its not recommended. It's recommended to declareSerialVersionUID
asprivate static final long
variable to avoid default mechanism.When you declare a class as
Serializable
by implementing marker interfacejava.io.Serializable
, Java runtime persist instance of that class into disk by using default Serialization mechanism, provided you have not customized the process usingExternalizable
interface.see also Why use SerialVersionUID inside Serializable class in Java
Code : javassist.SerialVersionUID
The docs for
java.io.Serializable
are probably about as good an explanation as you'll get:Original question has asked for 'why is it important' and 'example' where this
Serial Version ID
would be useful. Well I have found one.Say you create a
Car
class, instantiate it, and write it out to an object stream. The flattened car object sits in the file system for some time. Meanwhile, if theCar
class is modified by adding a new field. Later on, when you try to read (i.e. deserialize) the flattenedCar
object, you get thejava.io.InvalidClassException
– because all serializable classes are automatically given a unique identifier. This exception is thrown when the identifier of the class is not equal to the identifier of the flattened object. If you really think about it, the exception is thrown because of the addition of the new field. You can avoid this exception being thrown by controlling the versioning yourself by declaring an explicit serialVersionUID. There is also a small performance benefit in explicitly declaring yourserialVersionUID
(because does not have to be calculated). So, it is best practice to add your own serialVersionUID to your Serializable classes as soon as you create them as shown below:SerialVersionUID
is a unique identifier for each class,JVM
uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization.Specifying one gives more control, though JVM does generate one if you don't specify. The value generated can differ between different compilers. Furthermore, sometimes you just want for some reason to forbid deserialization of old serialized objects [
backward incompatibility
], and in this case you just have to change the serialVersionUID.The javadocs for
Serializable
say:Therefore, you must declare serialVersionUID because it give us more control.
This article has some good points on the topic.
SerialVersionUID is used for version control of object. you can specify serialVersionUID in your class file also. Consequence of not specifying serialVersionUID is that when you add or modify any field in class then already serialized class will not be able to recover because serialVersionUID generated for new class and for old serialized object will be different. Java serialization process relies on correct serialVersionUID for recovering state of serialized object and throws java.io.InvalidClassException in case of serialVersionUID mismatch
Read more: http://javarevisited.blogspot.com/2011/04/top-10-java-serialization-interview.html#ixzz3VQxnpOPZ