5
If I don’t declare that constant (serialVersionUID
) in a class that implements the interface Serializable
, I get a Warning. But what is this constant for anyway? Its value interferes with the serialization of the object?
5
If I don’t declare that constant (serialVersionUID
) in a class that implements the interface Serializable
, I get a Warning. But what is this constant for anyway? Its value interferes with the serialization of the object?
5
The serialVersionUID
is used to track the compatibility of serialized versions of the classes.
This is because if you serialize an instance of a class X
and saved in a file, and some time later changes the class X
and deserializes that instance, it may be that the deserialized data is not compatible with the new version of the class, since it has undergone a change.
And how do you define what the class version is? With serialVersionUID
. That’s what he’s for.
The idea is that if you create a serializable class A
, you declare something like private static final long serialVersionUID = 1L;
in it to define that this is the first version of the class. Then, if you make any structural changes incompatible with previous versions, you will change the serialVersionUID
for 2L
. In another change, it will change to 3L
and so on. Obviously, if the change is small and no incompatibility is introduced, you should not change the serialVersionUID
.
If you make an incompatible structural change in the class and do not change the serialVersionUID
, the deserialization of an incompatible instance will fail.
If you want to be able to read a different version of the class to be deserialized, you can implement the method readObject()
and there, read the serialVersionUID
necessary. The method readObjectNoData()
may also be useful.
Finally, if you do not define any serialVersionUID
, then one will be generated automatically for you from a hash taking into account the class name, superclasses, implemented interfaces and class members (fields, methods and constructs). That means it’s very easy to happen from serialVersionUID
automatically generated change even if no significant changes occur in the class. For this reason, it is recommended that you always put the serialVersionUID
explicitly.
Additional links for more information:
Java Object Serialization Specification - oracle.
Understanding the Series VersionUID - of Caelum.
Browser other questions tagged java interface serialization
You are not signed in. Login or sign up in order to post.
Victor without wanting to abuse his goodwill or extend the answer, what level of structural change could occur problems? Changing implementation within any method without changing its signature is considered a structural change that changes the
serialVersionUID
?– user28595
@diegofm Without changing the method structure, probably not. But adding some private method or changing the signature of an existing one is enough to change the
serialVersionUID
. Adding or changing an internal class can cause the compiler to create some package visibility methods to give external class access to the internal class, and this can also end up changing theserialVersionUID
. Therefore, in general, it is never safe to assume that theserialVersionUID
will not change if it is not explicitly defined.– Victor Stafusa