What is transient?

The transient keyword in Java is used to exclude a field from the serialization process. When an object is serialized, transient variables are not saved as part of the object's state. As a result, when the object is deserialized, transient fields are restored with their default values instead of their original values.

Key Points: • transient is used only with instance variables. • Transient fields are ignored during serialization. • It helps prevent sensitive or unnecessary data from being persisted. • After deserialization, transient variables receive default values. • Commonly used for passwords, temporary data, and calculated fields.

What is Serialization?

Serialization is the process of converting an object into a byte stream so that it can be:

• Stored in a file • Sent over a network • Cached • Persisted for later use

For serialization, a class must implement:

Serializable

Example:

class Employee implements Serializable {

}

Why Use transient?

Sometimes certain fields should not be saved.

Examples:

• Passwords • Security tokens • Session information • Temporary calculations • Cache data

In such cases, marking the field as transient prevents it from being serialized.

Example: Suppose we have a User object:

Username = john

Password = secret123

While saving the object, we may want to store the username but not the password.

The password can be declared as transient.

Code Example:

import java.io.Serializable;

class User implements Serializable {

    private String username;

    private transient String password;

    public User(

String username,

            String password) {

        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {

return "Username: " + username + ", Password: "

                + password;
    }
}

If the object is serialized and later deserialized:

Output:

Username: john

Password: null

Because password was marked as transient.

Default Values After Deserialization

When a transient variable is not serialized, it receives its default value after deserialization.

Data Type Default Value

int 0

long 0L

double 0.0

boolean false

Object Reference null

Example:

transient int age = 25;

After deserialization:

age = 0

Can transient Be Used with static?

Technically yes, but it has no effect.

Reason:

• static variables belong to the class. • Serialization only stores object state. • Static variables are never serialized.

Example:

private static String company;

Whether transient is used or not, static fields are ignored during serialization.

Common Use Cases

1. Password Storage

private transient String password;

2. Authentication Tokens

private transient String authToken;

3. Cached Data

private transient List<String> cache;

4. Calculated Values

private transient double totalAmount;

These values can be recreated when needed.

transient vs static

transient

• Excluded from serialization • Belongs to object instance

static

• Belongs to class • Not serialized by default

Both are ignored during serialization, but for different reasons.

Real-World Example

In an online banking application:

class Customer {

    private String accountNumber;

    private transient String otp;
}

During serialization:

• accountNumber is saved. • otp is ignored because it is temporary and security-sensitive.

Interview Tip: A concise interview answer is:

"The transient keyword is used to prevent a field from being serialized. When an object is serialized, transient variables are skipped and are restored with default values during deserialization. It is commonly used for sensitive information such as passwords, authentication tokens, and temporary data that should not be persisted."