SrcForge

Lesson 58

Transient Keyword

The transient keyword in Java is used to stop a field from being serialized when an object is converted into a byte stream. If a field is marked as transient, its value is not saved during serialization and will not be restored during deserialization.

Java Track

Save progress as you learn.

61 lessons

Lesson content

The transient keyword in Java is used to stop a field from being serialized when an object is converted into a byte stream. In simple terms, if a field is marked as transient, its value is not saved during serialization and will not be restored during deserialization.

What is the transient Keyword in Java?

In Java, transient is a keyword used with instance variables to exclude them from the serialization process.

When an object is serialized, non-transient fields are saved into the byte stream, while transient fields are skipped and not stored.

This is useful when a field contains sensitive data such as passwords, temporary values, derived or calculated data, or data that should not be persisted.

Key Terms

  • transient: A Java keyword used to prevent a field from being serialized.
  • Serialization: The process of converting an object into a byte stream.
  • Deserialization: The process of converting a byte stream back into a Java object.
  • Byte stream: A sequence of bytes used to store or transfer object data.

Why Use transient in Java?

The transient keyword is useful when you do not want certain object data to be stored or transferred.

Common Use Cases

  • Hiding confidential information like passwords or tokens.
  • Avoiding storage of temporary values.
  • Preventing unnecessary data from being written to a file.
  • Reducing the size of serialized objects.

How transient Works

When an object is serialized, Java only stores the values of fields that are eligible for serialization. If a field is marked as transient, Java ignores it during serialization.

Example idea: if an object has the fields name, age, and password, and password is marked as transient, then only name and age will be saved in the byte stream.

Important Points About transient

1. Transient Fields Are Not Serialized

Their values are skipped when the object is written to a stream.

2. Transient Fields Get Default Values After Deserialization

When the object is deserialized, transient fields are restored with Java default values:

  • 0 for int
  • 0.0 for double
  • false for boolean
  • null for objects and strings

3. Works Only with Instance Variables

The transient keyword is mainly used with non-static instance variables.

4. Often Used with Serialization

It is commonly used in classes that implement Serializable.

Example of transient in Java

Here is a simple example to understand how the transient keyword works:

import java.io.*;

class Student implements Serializable {
    String name = "Kumar";
    transient String password = "abc123";
}

public class TransientExample {
    public static void main(String[] args) throws Exception {
        Student s1 = new Student();

        // Serialization
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.ser"));
        out.writeObject(s1);
        out.close();

        // Deserialization
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("student.ser"));
        Student s2 = (Student) in.readObject();
        in.close();

        System.out.println("Name: " + s2.name);
        System.out.println("Password: " + s2.password);
    }
}

Expected Output

Name: Kumar
Password: null

Explanation

  • name is serialized and restored correctly.
  • password is marked as transient, so it is not saved.
  • After deserialization, password becomes null.

When to Use the transient Keyword

Use transient when a field contains sensitive information, is temporary, or can be recalculated.

  • Sensitive information: passwords, PINs, access tokens.
  • Temporary: session data, cache values, runtime-only flags.
  • Can be recalculated: computed totals, derived values, temporary processing results.

transient vs Normal Field

Field TypeSerialized?Value After Deserialization
Normal fieldYesOriginal saved value
transient fieldNoDefault value

Best Practices for Using transient

  • Use it for sensitive data — do not serialize passwords, secret keys, or confidential values.
  • Use it for temporary object state — mark a field transient if it is only needed while the program runs.
  • Do not use it for required business data — if the value must be restored later, it should not be transient.
  • Test serialization behavior — always verify what values are available after deserialization.

FAQ

1. What is the transient keyword in Java?

The transient keyword tells Java not to serialize a field when an object is converted into a byte stream.

2. What happens to a transient field after deserialization?

A transient field is restored with its default Java value, such as null, 0, or false.

3. Why is transient used in Java serialization?

It is used to exclude sensitive, temporary, or unnecessary data from the serialization process.

The transient keyword prevents a field from being serialized into a byte stream.
Transient fields are skipped during serialization and not stored.
After deserialization, transient fields receive default values: 0, 0.0, false, or null.
transient works only with non-static instance variables.
Use transient for sensitive, temporary, or recalculable data — not for required business data.
Normal fields retain their original saved value after deserialization; transient fields do not.

Where Java is used

Java in real-world development

transient

Keyword that prevents a field from being included in the serialized byte stream.

Default Value Restoration

After deserialization, transient fields are restored with Java default values such as null, 0, or false.

Sensitive Data

transient is commonly used to exclude passwords, PINs, and access tokens from serialization.

Temporary Data

transient is used for session data, cache values, and runtime-only flags that should not persist.

Derived Data

transient suits computed totals or derived values that can be recalculated after deserialization.

Instance Variables Only

The transient keyword applies only to non-static instance variables.