• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SerialVersionUID

 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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.


Thank you
Regards
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, assume that you have a class ObjectA

- 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.

http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html
http://java.sun.com/j2se/1.5.0/docs/api/java/io/ObjectOutputStream.html
http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/serialTOC.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic