Java serialization concept and Example Part II

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

In first part we have discussed about what is serialization and how to do the same in Java. In this article we will discuss some advance topics covering Serialization.

In this article we will use the same codes base we have provided in previous article.

Use of serialVersionUID

You must have seen a variable named “serialVersionUID” have been used in source code. There is a specific reason behind using this variable.

serialVersionUID is a version number associated to each serializable class by serialization runtime. This version number is used during deserialization process to verify that the sender and receiver of a serialized object have loaded class for that object which is compatible with respect to serialization.

  • Defining a serialVersionUID field in serializable class is not mandatory.
  • If a serializable class have 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. Which can vary based on compiler implementation. Hence it is advisable to define serialVersionUID.
  • It is advised to use private access modifier for 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.


Use of Transient

We can save the state of an object using Serializable. But what if i don’t want to save state of a field? In this case transient modifier can be used like below. Transient fields state will not be saved while serialization process and default value will be assigned to same variable while de-serialization.

Changing the Employee class with transient variable.

If you execute the same class(SerializaitonClass & DeserializationClass)  output will be different then previous code.

As you can see above last name is coming as null because state of that variable was not saved while serialization process.

Class Hierarchy and Serializable

Here i will discuss about the effect of Serializable interface on Class hierarchy. If a class has implemented Serializable interface then state of this class can be saved. But if same class extend another class which didn’t implement Serializable interface then Super class’s state will not be saved.

To see the difference we will update original Employee class. Now this class will extend another class superEmployee . This super class will not implement Serializable interface.

If you execute “SerializaitonClass” and “DeserializationClass” one after another then output would be like below

Transient vs Static variable

I have written a complete article on this. Please visit here.


Custom Serialization Process


Series Navigation<< Java serialization concept and ExampleTransient vs Static variable java >>

3 Comments Java serialization concept and Example Part II

Leave A Comment

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