Java transient modifier example
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:
- Ubuntu 12.04
- JDK 1.7.0.09
A simple class
Let's define a simple class containing a transient property:
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:
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:
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.