Java Serialization concept & Example Part II

In first part we have discussed what is serialization and how to achieve it in Java. In this article, we will discuss some advanced topics covering Serialization.

In this article, we will use the same codes base we have provided in the 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 receiver 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 the state of a field? In this case, a transient modifier can be used like below. Transient fields state will not be saved while serialization process and the default value will be assigned to the same variable while de-serialization.

Changing the Employee class with a transient variable.

package com.jbt;

import java.io.Serializable;

public class Employee implements Serializable
{
   public String firstName;
   /*
    * Here transient modifier is used for lastName variable. 
    * This variable's state will not be saved while serialzation.
    * While De-Serialization process default value will be provide.
    * null in this case.
    */
   transient public String lastName;
   private static final long serialVersionUID = 5462223600l;
}

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

Deserializing Employee...
First Name of Employee: Vivekanand
Last Name of Employee: null

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

Class Hierarchy and Serializable

Here I will discuss 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 superclass will not implement Serializable interface.

package com.jbt;

import java.io.Serializable;

public class Employee extends superEmployee implements Serializable {
	public String firstName;
	private static final long serialVersionUID = 5462223600l;
}

class superEmployee {
	public String lastName;
}

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

Deserializing Employee...
First Name of Employee: Vivekanand
Last Name of Employee: null

Transient vs Static variable

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

 

Custom Serialization Process

<TO_DO>

5 Comments Java Serialization concept & Example Part II

    1. Vivekanand

      It means that if you are declaring an array, it can not have serialVersionUID. Because it might have created an issue that’s why it is completely ignored by the creator. And in general you cannot assign one so I believe u should ignore this statement altogether.

      Reply

Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.