| Author |
serialVersionUID
|
Amirtharaj Chinnaraj
Ranch Hand
Joined: Sep 28, 2006
Posts: 215
|
|
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
Joined: Nov 24, 2005
Posts: 14672
|
|
|
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).
|
[My Blog]
All roads lead to JavaRanch
|
 |
Amirtharaj Chinnaraj
Ranch Hand
Joined: Sep 28, 2006
Posts: 215
|
|
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
Joined: Feb 28, 2007
Posts: 1707
|
|
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
|
cmbhatt
|
 |
Amirtharaj Chinnaraj
Ranch Hand
Joined: Sep 28, 2006
Posts: 215
|
|
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
Joined: Feb 28, 2007
Posts: 1707
|
|
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
Joined: Sep 28, 2006
Posts: 215
|
|
yes chandra your sample is simply awesome thanks indeed regards amir
|
 |
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
|
|
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.
|
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
|
 |
 |
|
|
subject: serialVersionUID
|
|
|