What is the use of serialVersionUID

This entry is part 31 of 34 in the series Core Java Course

Here I will discuss the importance of the variable serialVersionUID which are used in Serializable classes. 

Below is an example that will make you understand the exact use of the variable.

Example Code


SerializaitonClass.java (This class will be used to serialize)


DeserializationClass.java (This class will be used to deserialize )


Now execute “SerializationClass.java” and then “DeserializationClass.java”. It will first create a file with Employee object’s state and then while de-serialization it creates object from the same file.  Output will be something like below.

Now let’s try and remove “serialVersionUID” variable from Employee.java file and again run “SerializationClass.java”  file.  It will create “employee.txt”  file again with  the object’s state .  Now let’s add a new variable in Employee class  suppose  String Address.


Now run “DeserializationClass.java” and see the output.Booom

It will throw an incompatible exception. Because the given class Employee.java was changed in between serialization and de-serialization process. Hence the system failed to identify that it is still the same class. To make our system understand that it is the same class you have to make use of serialVersionUID  variable inside class.

You can try follow the above steps but keep serialVersionUID intact and see the output. De-serialization process will work without any issue.

Bullet Points

  • Defining a serialVersionUID field in serializable class is not mandatory.
  • If a serializable class has an explicit serialVersionUID then this field should be of type long and must be static and final.
  • If there is no serialVersionUID field defined explicitly then serialization runtime will calculate default value for that class. The value can vary based on compiler implementation. Hence it is advisable to define serialVersionUID.
  • It is advised to use private access modifier for serialVersionUID.
  • Different class can have same serialVersionUID.
  • Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.
  • If there is a difference between serialVersionUID of loaded reciever class and corresponding sender class then InvalidClassException will be thrown.
  • You should use different serialVersionUID for different version of  same class if you want to forbid serialization of new class with old version of same class.



If you do not provide serialVersionId in a class which is supposed to be serialised then compiler will give warning  messages about the same. If you want to override this warning you can use given annotation. Once used, compiler will stop complaining about the missing serialVersionUID.

Series Navigation<< Transient vs Static variable javaJava Thread Tutorial >>

3 Comments What is the use of serialVersionUID

Leave A Comment

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