serialVersionUID is used to ensure that same class(That was used during Serialization) is loaded during Deserialization.serialVersionUID is used for version control of object.
If you have used Serialization then You might have seen serialVersionUID because whenever you implement Serializable interface your IDE will give you warning. Pirates of the caribbean game.
![]() Java Serialization Tutorial:
Nov 05, 2013 Persistence literal meaning is long live. The advantage of data persistence is when the serialized object is read back, it is guaranteed to return the same data. With this knowledge, now let us know what classes exist in Java API to support serialization and then examples on Serialization.
Serialversionuid Syntax:
As per java docs
serialVersionUID must be Static and final.You can assign any number to it.
Lets see an example: Create Employee.java in src->org.arpit.javapostsforlearning
1.Employee.java
Create SerializeMain.java in src->org.arpit.javapostsforlearning
2.SerializeMain.java
Create DeserializeMain.java in src->org.arpit.javapostsforlearning
4.Run it:
First run SerializeMain.java then DeserializeMain.java and you will get following output:
So when you run program,it was completed successfully and employee.ser has been created on disk.If you again run DeserializeMain.java,it will again run successfully. Now change value of variable serial to
and if you now run DeserializeMain.java it will give you following error.
So here during deserialization,we got error.It complained about Serialvesionuid being changed.But how does it know? because serialversionuid is a static variable and we know that “We can not serialize static variables”.How does it store serialversionuid? yes ,there is exception.Inspite of serialversionuid being static,it get serialized.So ObjectOutputStream writes every time to output stream and ObjectInputStream reads it back and if it does not have same values as in current version of class then it throw InvalidClassException.
Why serialversionuid is required?
In real time,It is possible that you have serialized a object in a file and you deserialized it after few months on different JVM.In between serialization and deserialization class declaration has been changed.So it is a good idea to maintain version system and serialversionid does exactly same thing.It checks if you are deserializing same object which you have serialized.
Best Practices:
“the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization”.
So it says you must declare serialVersionUID because it give us more control.for e.g. Default rules for generating serialVersionUID can be too strict in some cases. For example when the visibility of a field changes, the serialVersionUID changes too. or sometimes you just want to forbid deserialization of old serialized object then you can just change serialVersionUID.
Is this enough?.No,you must not only declare it but also maintain it.So most important part is maintaining serialVersionUID otherwise every thing will run without any exceptions.You should change serialVersionUID when there is some change in the definition of data stored in the class for example data type of field is changed.
Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.
The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.
To make a Java object serializable we implement the java.io.Serializable interface.
The ObjectOutputStream class contains writeObject() method for serializing an Object.
The ObjectInputStream class contains readObject() method for deserializing an object.
Advantages of Serialization
1. To save/persist state of an object. 2. To travel an object across a network.
Only the objects of those classes can be serialized which are implementing java.io.Serializable interface.
Serializable is a marker interface (has no data member and method). It is used to “mark” java classes so that objects of these classes may get certain capability. Other examples of marker interfaces are:- Cloneable and Remote.
Points to remember
1. If a parent class has implemented Serializable interface then child class doesn’t need to implement it but vice-versa is not true. 2. Only non-static data members are saved via Serialization process. 3. Static data members and transient data members are not saved via Serialization process.So, if you don’t want to save value of a non-static data member then make it transient. 4. Constructor of object is never called when an object is deserialized. 5. Associated objects must be implementing Serializable interface. Example :
SerialVersionUID
The Serialization runtime associates a version number with each Serializable class called a SerialVersionUID, which is used during Deserialization to verify that sender and reciever of a serialized object have loaded classes for that object which are compatible with respect to serialization. If the reciever has loaded a class for the object that has different UID than that of corresponding sender’s class, the Deserialization will result in an InvalidClassException. A Serializable class can declare its own UID explicitly by declaring a field name. It must be static, final and of type long. i.e- ANY-ACCESS-MODIFIER static final long serialVersionUID=42L;
If a serializable class doesn’t explicitly declare a serialVersionUID, then the serialization runtime will calculate a default one for that class based on various aspects of class, as described in Java Object Serialization Specification. However it is strongly recommended that all serializable classes explicitly declare serialVersionUID value, since its computation is highly sensitive to class details that may vary depending on compiler implementations, any change in class or using different id may affect the serialized data.
It is also recommended to use private modifier for UID since it is not useful as inherited member.
serialver
The serialver is a tool that comes with JDK. It is used to get serialVersionUID number for Java classes. You can run the following command to get serialVersionUID
serialver [-classpath classpath] [-show] [classname…]
Example 1:
This is Mercari. The fast and easy way to sell or buy almost anything. List your item in minutes. Got something you don’t use, never used or just outgrew? Sep 25, 2019 Mercari: The Selling App. This is Mercari. The fast and easy way to sell or buy almost anything. From fashion to toys, sporting goods to electronics, jewelry to shoes - all the brands you know and love. It’s a snap - list your item in minutes. Got something you don’t use, never used or simply outgrew? SELLING IS FAST & EASY. Mercury shopping appraisal.
Output :
Example 2:
Java Serialization Data Version 5 1
Output:
This article is contributed by Mehak Narang and Shubham Juneja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Java Serialization Data Version 5 7Recommended Posts:How To Read Java Serialization Data Version 5Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |