• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

serialVersionUID

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes chandra

your sample is simply awesome

thanks indeed
regards
amir
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic