Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you need to redefine serialVersionUID if you extends a class which implements Serializable "down the line"?

For one of my projects, I need to define a new exception which extends ProviderMismatchException.

From the javadoc link, you can see that this exception:

  • extends IllegalArgumentException, which
  • extends RuntimeException, which
  • extends Exception, which
  • extends Throwable.

All of them define their own static final serialVersionUID except for Throwable which adds the private modifier.

Now, if you implement an interface Foo, then all inherited classse also implement that interface, and this stands for Serializable as well; however, why do subclasses in the JDK redefine it for each subclass? What is the danger of not defining it again for inherited classes?

like image 677
fge Avatar asked Dec 12 '15 22:12

fge


People also ask

What is serialVersionUID and why do we need it?

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.

What is the role of serialVersionUID in serialization process?

Simply put, we use the serialVersionUID attribute to remember versions of a Serializable class to verify that a loaded class and the serialized object are compatible. The serialVersionUID attributes of different classes are independent. Therefore, it is not necessary for different classes to have unique values.

What is serialVersionUID what would happen if you don't define this?

The SerialVersionUID can be used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible w.r.t serialization. If the deserialization object is different than serialization, then it can throw an InvalidClassException.

Is Serializable consider declaring a serialVersionUID?

However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during ...


1 Answers

The serialVersioUID is a static long value, thus it won't be inherited through the class hierarchy.

You need this to indicate if a prior serialized instance of a class has the same version as the current implementation or not.

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization.

For Details see here:

What is a serialVersionUID and why should I use it?

like image 189
Marcinek Avatar answered Sep 25 '22 14:09

Marcinek