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:
IllegalArgumentException
, whichRuntimeException
, whichException
, whichThrowable
.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?
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.
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.
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.
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 ...
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With