What is the use of Java Transient Keyword – Serailization Example

In this article we will learn about the usage of Transient Keyword in Serialization. Before getting into, some basic knowledge on How Serialization works in Java is required. Lets take an example we have a class Employee which is having three data members empId, empName, empSalary. Suppose if you dont want empSalary alone not to be serialized(non-persistant) then you can mark empSalary as transient

Employee.java

package com.javainterviewpoint;

import java.io.Serializable;

public class Employee implements Serializable
{

      private int empId;
      private String empName;
      private int empSalary;
 
 public int getEmpId() {
       return empId;
 }

 public String getEmpName() {
       return empName;
 }
 
 public int getEmpSalary() {
       return empSalary;
 }

 public Employee(int empId,String empName,int empSalary)
 {
       this.empId=empId;
       this.empName=empName;
       this.empSalary=empSalary;
 }
}

In our Employee class we have three data members empId, empName, empSalary

SerializationUtility.java

package com.javainterviewpoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializationUtility
{
    //Method to serialize and save the object in the file
    public void serialize(Object obj,String filePath)
    {
        try
        {
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
            outputStream.writeObject(obj);
            outputStream.flush();
            outputStream.close();
        }
        catch(Exception ee)
        {
            ee.printStackTrace();
        }
    }
    
    //Method to deserialize and return the object
    public Object deSerialize(String filePath)
    {
        Object obj = null;
        try
        {
            FileInputStream fileInputStream = new FileInputStream(filePath);
            ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
            obj = inputStream.readObject();
        }
        catch(Exception ee)
        {
            ee.printStackTrace();
        }
        return obj;
    }
}

We have used java.util.ObjectOutputStream and java.util.ObjectInputStream to write/read object to/from the file “Persist.txt”

Our SerializationUtility class has two methods

  • serialize() : we will make use of java.util.ObjectOutput stream to write the Object which we pass, to the file “Persist.txt”
  • deSerialize(): java.util.ObjectInputStream is used to read the Object from the file and return it back to the user.

SerilizationTest.java

 package com.javainterviewpoint;

public class SerilizationTest 
{
      public static void main(String args[])
     {
          //Path to store the Serialized object
          String filePath="D://Persist.txt";
          Employee emp = new Employee(1,"JavaInterviewPoint",1000);
 
         System.out.println();
 
         SerializationUtility su = new SerializationUtility();
 
         //Serialize emp object
         su.serialize(emp, filePath);
 
         //De-Serialize Employee object
         Employee ee = (Employee)su.deSerialize(filePath);
         System.out.println("Employee id : "+ee.getEmpId());
         System.out.println("Employee Name : "+ee.getEmpName());
        System.out.println("Employee Salary : "+ee.getEmpSalary());
    }
}

Output

When we run our SerializationTest class the serialize() and deSerialize() methods will be called and below output will be rendered.

Employee id : 1
Employee Name : JavaInterviewPoint
Employee Salary : 1000

Now we will slightly modify our Employee class to have a transient variable (empSalary)

package com.javainterviewpoint;

import java.io.Serializable;

public class Employee implements Serializable
{

      private int empId;
      private String empName;
      private transient int empSalary;
 
 public int getEmpId() {
       return empId;
 }

 public String getEmpName() {
       return empName;
 }
 
 public int getEmpSalary() {
       return empSalary;
 }

 public Employee(int empId,String empName,int empSalary)
 {
       this.empId=empId;
       this.empName=empName;
       this.empSalary=empSalary;
 }
}

Again when we run our SerializationTest class, we will get empSalary as “0”, as empSalary is transient and hence the value will not be persisted.

Employee id : 1
Employee Name : JavaInterviewPoint
Employee Salary : 0

Leave a Reply

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