Possible Duplicates:
Why should I bother about serialVersionUID?
what is a serial version id?
When i created a new class extending java.util.logging.Level in Eclipse, It asked me to add a default or generated serial version ID. I just added blindly without knowing what it is and why I have to add it.
Can anybody tell me what is it and why it its required.
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.
Technically you can't prevent two classes from having the same serial version UID, much like you can't prevent two objects from having the same system hash code.
The serialVersionUID is a universal version identifier for a Serializable class. Deserialization uses this number to ensure that a loaded class corresponds exactly to a serialized object. If no match is found, then an InvalidClassException is thrown.
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 Serial Version ID is used when serializing and deserializing an object. Java recognizes if the bytes you want to deserialize match the local class version. If not it will throw an exception.
This is important when doing RMI or persisting object structures.
There's a very good description about serializing in the javadoc of Serializable.
It is the unique identifier for the class, used for serialization.
It is wise to declare it if you're serializing asap because if you don't declare one then when changing the class it will get a different one generated automatically and the serialization will stop working.
A good reference is here: http://c2.com/ppr/wiki/JavaIdioms/AlwaysDeclareSerialVersionUid.html
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