Java serialization concept and Example Part II

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.


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 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.


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>

Series Navigation<< Java serialization concept and ExampleTransient vs Static variable java >>
By | 2013-12-28T16:08:56+00:00 December 28th, 2013|Core Java|3 Comments

About the Author:

3 Comments

  1. Aarushi May 8, 2016 at 6:06 am - Reply

    Nice article.. well explained!

  2. shalini July 14, 2016 at 7:57 am - Reply

    Very well explained!!

  3. Niraj September 21, 2016 at 12:40 am - Reply

    Good one

Leave A Comment