class A implements Serializable{ private static final long serialVersionUID = 5L; ... }
and
class B implements Serializable{ private static final long serialVersionUID = 6L; ... }
then it is necessary to give unique serialVersionUID
to both classes.
So can I assign serialVersionUID = 5L
for both classes?
I read following links
Why generate long serialVersionUID instead of a simple 1L?
What is a serialVersionUID and why should I use it?
Yes, you can. Serial versions of different classes are independent and do not interfere each other. Eclipse even proposes you to set serialVersionID by default value that is 1L .
You should usually have the serialVersionUID as a private static final field in your class; you can choose any value. If you don't specify one, a value is generated automatically (this approach can lead to problems as it is compiler (object structure) dependent.
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.
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.
Yes, you can. Serial versions of different classes are independent and do not interfere each other.
PS
Eclipse even proposes you to set serialVersionID
by default value that is 1L
.
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