I wanted to know wht exactly the Serial Version UId is all about. I did google for the information but i got a very abstract answers like it improves the performance N stuff.. Please let me know where exactly we use it.
When a object is deserialized , first it checks that class compatibility with the class available to the classloader at the instance of deserialization.This is done by comparing the serialversionID of the class that is there with the serialized data to that of the serialversionID class which would be used for deserialization.
It might happen that class used for creating the object before serialization has changed in the couse of time. HF book on core java explains this fact very clearly.You can refer that.
- You want to store the object into a file storage. You make use of writeObject and readObject to do that. You have successfully serialized the objects to disk storage.
- Now you change the class ObjectA, say you add new member variable or delete existing variable. You compile the class. Since your class implements Serializable interface a new SUID is generated.
- After this, you want to read the contents back from disk which you serialized before. But you can't since the class ObjectA has been modified java will throw an exception.
- So how do you make deserialize the data from disk with changed class definition? With help of SUID. You declare private static final long serialVersionUID=<Original SUID of ObjectA> in the class ObjectA.
In short SUID are used when the version of class reading the data from file is different from the version of class that wrote the data to file.
The below URL's will help in understanding serialization and SUID's.