File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

serialVersionUID

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

i didnt understand the reason for having serialVersionUID in

a class that implemts serializable interface

looking for your replies

regards
amir
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It basically tells what version the class is. When you serialize an instance, you want to deserialize it with the same version of the class. If the version does not match, an exception will be thrown (InvalidClassException I think).
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have gone through this link


http://c2.com/ppr/wiki/JavaIdioms/AlwaysDeclareSerialVersionUid.html

but i am not satisfied by the explaination

regards
amir
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amrithraj,

You do the following to understand serialVersionUID.

1- Serialize instances of your class.
2- Change the class definition, add method(s), remove member variables or add
3- Try to deserialize the instances you serialized in the step 1
You will get InvalidClassException because while serialization takes place, some information about your class in also saved as part of serialization.

OK!
serialVerisonUID at your rescue!
1- Define serialVersionUID in your class!
2- Now serialize the instances
3- Make chages to your class
4- Deserialize the instances, Aren't you happy because this time no InvalidException occured, because serialization process didn't take trouble
to generate serialVersionUID because you have defined it, and it is same (make sure you have not changed this)


Thanks and Regards,
cmbhatt
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi chandra

i would like to thank you for your reply

i will be very much thankful to you if you send

me a sample for doing this

i tried this by writing sample java class

i make the instance of the class serializable by implementing

serializable interface ,then how can i get them deserialize

regards
amir
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amrithraj,

Sure I have written the code demonstrating the use of serialVersionUID for you,
Look at the following code,





/*
* Once you have defined serialVersionUID for your
* class, the Java is not going to compute it for you;
* As a result you can change the definition of a class
* by adding removing methods, members or so while
* deserializing. The JVM wont show you error that
* serialVerionUID mismatch. Because you have constant
* serialVersionUID at every execution of your code.
*/



Got it Amrithraj? Any doubt please post?



Regards,
cmbhatt
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes chandra

your sample is simply awesome

thanks indeed
regards
amir
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The version information is written into the serialisation stream, to ensure that one version of your code doesn't try and fail to read data written by another version.

By default, the version changes whenever fields or methods change in your code. So any slight change causes a failure.

However, by defining the version yourself using the serialVersionUID field, you can tell Java whether you believe the streams are compatible. Very often, changes to methods have no effect on whether the streams are compatible, and you want to ensure Java understands this, by having a serialVersionUID that doesn't change when methods change.

Sometimes, streams are compatible even when fields have changed. A field that's in the stream but not in the local class will be discarded. A field that's not in the stream but is in the local class will be initialised to its default. If you know that your code will cope with this, then use serialVersionUID to tell Java the streams are compatible.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic