← Back to Questions
Java

What is transient keyword in Java?

Learn What is transient keyword in Java? with simple explanations, real-time examples, interview tips and practical use cases.

What is transient Keyword in Java?

The transient keyword in Java is used to prevent a variable from being serialized.

In simple words:

When an object is serialized, transient variables are skipped and not saved into the byte stream.


Why transient Keyword is Important?

transient is mainly used for:

  • Protecting sensitive data
  • Improving serialization performance
  • Avoiding unnecessary data storage
  • Preventing serialization of temporary values

transient Keyword Overview Diagram


Java Object

      |
      +-------> Normal Variables → Serialized

      |
      +-------> transient Variables → Ignored


Basic Example

import java.io.*;

class User
implements Serializable {

    String username;

    transient String password;

}

What Happens During Serialization?

  • username gets serialized
  • password gets skipped

Serialization Flow


User Object

      |
      +-------> username Saved

      |
      +-------> password Ignored


Deserialization Result

After deserialization:

password = null

Complete Example

import java.io.*;

class User
implements Serializable {

    String username;

    transient String password;

}

public class Demo {

    public static void main(String[] args)
    throws Exception {

        User u = new User();

        u.username = "Naresh";

        u.password = "secret123";

        FileOutputStream fos =
            new FileOutputStream(
                "user.ser"
            );

        ObjectOutputStream oos =
            new ObjectOutputStream(fos);

        oos.writeObject(u);

        FileInputStream fis =
            new FileInputStream(
                "user.ser"
            );

        ObjectInputStream ois =
            new ObjectInputStream(fis);

        User user =
            (User) ois.readObject();

        System.out.println(
            user.username
        );

        System.out.println(
            user.password
        );

    }

}

Output

Naresh
null

Why password Became null?

Because transient variables are not serialized.


Internal Working Flow


Object Serialization Starts

      |
      v

Serializable Variables Processed

      |
      +-------> Normal Variables Saved

      |
      +-------> transient Variables Ignored


Where is transient Used?

  • Password fields
  • OTP values
  • Session tokens
  • Temporary calculations
  • Cache references
  • Sensitive information

Real-Time Example

class BankUser
implements Serializable {

    String accountNumber;

    transient String pin;

}

Why Important?

PIN should never be stored inside serialized file.


Difference Between transient and static

Feature transient static
Purpose Skip Serialization Shared Class Variable
Belongs To Object Class
Serialized? No No
Memory Location Heap Method Area

Difference Between transient and final

Feature transient final
Purpose Skip Serialization Constant/Immutable
Modification Allowed? Yes No
Serialization Impact Ignored Serialized Normally

Can transient Be Used with static?

Yes, but it is unnecessary because static variables are already not serialized.


Example

transient static int x;

Important Interview Point

transient works only during:

Serialization

Without Serialization?

transient has no special effect.


What Happens During Deserialization?

transient variables get default values.


Default Values Table

Data Type Default Value
int 0
boolean false
Object null
double 0.0

Example

transient int age;

After Deserialization

age = 0

Can We Serialize transient Variables Manually?

Yes, using:

  • writeObject()
  • readObject()

Custom Serialization Example

private void writeObject(
    ObjectOutputStream out
) throws Exception {

    out.defaultWriteObject();

    out.writeObject(password);

}

Custom Deserialization Example

private void readObject(
    ObjectInputStream in
) throws Exception {

    in.defaultReadObject();

    password =
        (String) in.readObject();

}

Serialization and Security

transient helps protect sensitive information from accidental storage or transmission.


Security Flow


Sensitive Data

      |
      v

Marked transient

      |
      v

Serialization Happens

      |
      v

Sensitive Data Excluded


transient in Banking Systems

Banking applications use transient for:

  • ATM PINs
  • OTP values
  • Session tokens
  • Authentication secrets

Banking Example

class Customer
implements Serializable {

    String accountId;

    transient String otp;

}

Why Important?

Sensitive banking data should not be persisted.


transient in E-Commerce Systems

E-commerce applications use transient for:

  • Temporary payment tokens
  • Session IDs
  • Temporary cart calculations
  • Secure checkout data

transient in Spring Boot

Spring Boot applications use transient for:

  • DTO temporary fields
  • Session-sensitive data
  • JWT-related temporary data
  • Cache-only values

Spring Boot Example

class UserDTO {

    String username;

    transient String jwtToken;

}

transient in Microservices

Microservices architectures use transient for:

  • Temporary request metadata
  • Distributed tracing values
  • Sensitive API data
  • Internal cache references

Microservice Flow


DTO Created

      |
      v

Sensitive Fields Marked transient

      |
      v

Serialized for Kafka/API

      |
      v

Sensitive Data Excluded


Advantages of transient

  • Improves security
  • Reduces serialized size
  • Prevents sensitive data leaks
  • Improves serialization performance

Disadvantages of transient

  • Data lost after deserialization
  • Requires custom serialization if restoration needed

Common Interview Mistake

Many developers think transient variables are permanently deleted.

Actually:

  • They exist normally in memory.
  • Only serialization ignores them.

Another Common Mistake

Many developers think transient works without serialization.

Actually:

  • transient only affects serialization process.

Best Practices

  • Use transient for sensitive fields
  • Never serialize passwords directly
  • Use custom serialization carefully
  • Document transient fields properly
  • Validate deserialized objects

Realtime Enterprise Example

Secure Banking Session


Customer Object Created

      |
      v

OTP Stored as transient

      |
      v

Object Serialized

      |
      v

OTP Not Saved

      |
      v

Security Improved


Related Learning Topics


Professional Interview Answer

The transient keyword in Java is used to prevent instance variables from being serialized. When an object is serialized using the Serializable interface, transient variables are skipped and not included in the byte stream. After deserialization, transient variables receive default values such as null, 0, or false. transient is commonly used for sensitive data like passwords, OTPs, authentication tokens, temporary calculations, and cache references. Enterprise systems, Spring Boot applications, banking platforms, cloud-native microservices, Kafka messaging systems, and distributed architectures frequently use transient to improve security, reduce serialized object size, and prevent accidental data exposure.


Frequently Asked Questions

What is transient keyword in Java?

transient prevents a variable from being serialized.

Are transient variables stored after serialization?

No, transient variables are ignored during serialization.

What happens after deserialization?

transient variables receive default values.

Can transient be used with static?

Yes, but it is unnecessary because static variables are already not serialized.

Why is transient used?

It is mainly used for sensitive or temporary data.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.