serialVersionUID in java Serialization

This is 2nd part of java Serialization tutorial

Java Serialization Tutorial:

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.

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
3.DeserializeMain.java

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:

Java docs says:
“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.

Related Posts

  • Serialization interview questions
    14 February

    Java Serialization interview questions and answers

    Serialization is one of most important concept in java. If you are going to face core java interview, then you might be asked some questions from Serialization. Java Serialization Tutorial: Serialization in java Java Serialization interview questions and answers serialversionuid in java serialization externalizable in java Transient keyword in java Difference between Serializable and Externalizable […]

  • 14 February

    Difference between Serializable and Externalizable in Java

    In this tutorial,  we are going to see differences between Serializable and Externalizable interface in Java. Java Serialization Tutorial: Serialization in java Java Serialization interview questions and answers serialversionuid in java serialization externalizable in java Transient keyword in java Difference between Serializable and Externalizable in Java Before understanding difference, let me provide a short note on Serializable […]

  • 19 May

    Java transient keyword with example

    Transient variable is the variable whose value is not serialized during serialization. You will get default value for these variable when you deserialize it. Lets say you have Country class and you don’t want to Serialize population attribute as it will change with time, so you can declare population attribute as transient and it won’t […]

  • 26 February

    Externalizable in java

    This is 3rd part of java Serialization tutorial Java Serialization Tutorial: Serialization in java Java Serialization interview questions and answers serialversionuid in java serializationexternalizable in java Transient keyword in java Difference between Serializable and Externalizable in Java Before understanding Externalizable interface, you need to have idea about Serialization.You can read more about Serialization at Serialization […]

  • 09 March

    Serialization in java

    This is 1st part of java Serialization tutorial. Java Serialization Tutorial: Serialization in java Java Serialization interview questions and answers serialversionuid in java serializationexternalizable in java Transient keyword in java Difference between Serializable and Externalizable in Java Java provides mechanism called serialization to persists java objects in a form of ordered or sequence of bytes […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.