What is transient variable in Java - Serialization Example

What is transient variable in Java
transient variable in Java is a variable whose value is not serialized during Serialization and which is initialized by its default value during de-serialization, for example for object transient variable it would be null. this behavior can be customized by using Custom Serialized form or by using Externalizable interface. transient variable is used to prevent any object from being serialized and you can make any variable transient by using transient keyword. By the way difference between transient and volatile variable in Java is a famous Java interview question but transient variable is completely different than volatile variable which we have discussed in our post What is volatile variable in Java.

In next section we will see complete example of serialization where we will first serialize an instance of Book class which implements Serializable and than de-serialize to see What is the value of transient variable after deserialization.

How to use transient variable in Java - Serialization Example

What is transient variable in Java Serialization ExampleHere is a complete code example of  Serialization in Java which demonstrate How to use transient variable in Java program; transient variables are not serialized during Serialization process and initialize with default value during deserialization.

package test;

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

 * Java program to demonstrate What is transient variable in Java and fact that value of
 * transient variable is not serialized and during serialization it initialized with
 * default value of that data type. e.g. If transient variable is Object than after
 * deserialization its value would be null.
 * @author Javin

public class TransientTest {

    public static void main(String args[]) {
       Book narnia = new Book(1024, "Narnia", "unknown", 2);
       System.out.println("Before Serialization: " + narnia);
        try {
            FileOutputStream fos = new FileOutputStream("narnia.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            System.out.println("Book is successfully Serialized ");

            FileInputStream fis = new FileInputStream("narnia.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Book oldNarnia = (Book) ois.readObject();
            System.out.println("Book successfully created from Serialized data");
            System.out.println("Book after seriazliation : " + oldNarnia);
        } catch (Exception e) {


 * A class which implements Serializable interface and has a transient variable.

class Book implements Serializable{
    private int ISBN;
    private String title;
    private String author;
    private transient int edition = 1; //transient variable not serialized

    public Book(int ISBN, String title, String author, int edition) {
        this.ISBN = ISBN;
        this.title = title;
        this.author = author;
        this.edition = edition;

    public String toString() {
        return "Book{" + "ISBN=" + ISBN + ", title=" + title + ", author=" + author + ", edition=" + edition + '}';

Before Serialization: Book{ISBN=1024, title=Narnia, author=unknown, edition=2}
Book is successfully Serialized
Book successfully created from Serialized data
Book after seriazliation : Book{ISBN=1024, title=Narnia, author=unknown, edition=0}

If you look at this example of Serializing Object in Java you will realize that value of transient variables are not serialized and persisted and during deserialization, those values are initialized with there default value which is zero in case of int variable. Since constructor also didn't run during de-serialization it won't get the value provided during constructor. In Summary use transient variable carefully in Java.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

Other post you may like :


Feel free to comment, ask questions if you have any doubt.