Java transient modifier example

22 January 2013
By Gonçalo Marques
In this tutorial you will learn how to use the transient keyword as a property modifier in Java.

Introduction

The transient keyword may be used as a property modifier in Java and is directly related with serialization. In short the properties declared as transient are not serialized by the default serialization mechanism. This may be useful when we have properties that represent data that we don't want to persist or transmit over the network (when an object is sent over the network it's also serialized into a stream of bytes). We may think of dynamic data that was calculated after some instance initialization and is subject to change often, so it doesn't make sense to persist, or sensitive information like authentication tokens that only make sense in the current execution context. These conditions are only examples as the need of transient usage is very specific for different scenarios.

Environment:

  1. Ubuntu 12.04
  2. JDK 1.7.0.09

A simple class

Let's define a simple class containing a transient property:

TestClass.java

package com.byteslounge.serialization;

import java.io.Serializable;

public class TestClass implements Serializable {

  private static final long serialVersionUID = 8191670218412460916L;
  
  // will be serialized
  private String propertyOne;

  // will not be serialized
  private transient String propertyTwo;
  
  public TestClass(String propertyOne, String propertyTwo) {
    this.propertyOne = propertyOne;
    this.propertyTwo = propertyTwo;
  }
  
  public String getPropertyOne() {
    return propertyOne;
  }
  
  public String getPropertyTwo() {
    return propertyTwo;
  }
  
}

Note that we are implementing the Serializable interface. This is required to make TestClass serializable. Property propertyTwo is declared as transient so it will not be serialized.

Testing

Let's create a simple test class:

Main.java

package com.byteslounge.serialization;

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

public class Main {

  public static void main(String[] args) 
    throws Exception {
    
    TestClass testWrite = new TestClass("valueOne", "valueTwo");
    FileOutputStream fos = new FileOutputStream("testfile");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(testWrite);
    oos.flush();
    oos.close();
      
    TestClass testRead;
    FileInputStream fis = new FileInputStream("testfile");
    ObjectInputStream ois = new ObjectInputStream(fis);
    testRead = (TestClass)ois.readObject();
    ois.close();
      
    System.out.println("--Serialized object--");
    System.out.println("propertyOne: " + testWrite.getPropertyOne());
    System.out.println("propertyTwo: " + testWrite.getPropertyTwo());
    System.out.println("");
    System.out.println("--Read object--");
    System.out.println("propertyOne: " + testRead.getPropertyOne());
    System.out.println("propertyTwo: " + testRead.getPropertyTwo());

  }

}

When we run the test the following output is generated:

--Serialized object--
propertyOne: valueOne
propertyTwo: valueTwo

--Read object--
propertyOne: valueOne
propertyTwo: null

Property propertyTwo was not serialized so when we read our instance back propertyTwo value is null.

The example source code is available at the end of this page.

Download source code from this article

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: https://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: