but i am not satisfied by the explaination
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.
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,
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
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?
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.