This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes SerialVersionUID Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "SerialVersionUID" Watch "SerialVersionUID" New topic
Author

SerialVersionUID

Ranganath Junpal
Ranch Hand

Joined: Aug 31, 2006
Posts: 76
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

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: SerialVersionUID
 
Similar Threads
Passing References and Passing Values
Technology stack problem(jquery,struts,spring,hibernate)
Salary Range - SCJP+SCJD+SCEA
JPA Annotation
B&S long recordnumber dilemma