Back to Serialization & transient
Q1Question 1 of 11

What is the transient keyword?

Key Concepts
  • transient marks a field to be EXCLUDED from serialization
  • On deserialization, transient fields are set to their default value (0, null, false)
  • Use for: passwords, cached values, derived data, non-serializable fields
  • transient does NOT affect the field during normal program execution – only during serialization
  • Understanding this concept is essential for Java interviews at senior level

Definition

transient marks a field to be excluded from serialization. On deserialization, it is set to its default value (0, null, false).

transient behavior:

  • During serialization: The field is skipped. Not written to the byte stream.
  • During deserialization: The field is set to its type's default: 0 for numbers, null for objects, false for boolean.
  • During normal execution: transient has no effect. The field works like any other field.

transient is like a "do not photocopy" stamp on a confidential page in a bank file. When the file is copied (serialized), that page is left blank. The original file still has it.

Understanding the Concepts

How transient works

JAVA
public class Account implements Serializable {
    private String accountId;              // serialized
    private BigDecimal balance;            // serialized
    private transient String password;     // NOT serialized
    private transient BigDecimal cachedInterest;  // NOT serialized
}

// After deserialization:
// accountId = "SAV2001" (restored)
// balance = 500000 (restored)
// password = null (default, was transient)
// cachedInterest = null (default, was transient)

When to use transient

  • Sensitive data: Passwords, PINs, security tokens.
  • Cached/derived data: Values that can be recomputed from other fields.
  • Non-serializable fields: Logger instances, database connections, thread references.
JAVA
public class TransferService implements Serializable {
    private transient Logger log = Logger.getLogger("TransferService");  // non-serializable
    private transient Connection dbConnection;  // non-serializable
}

What should a senior engineer know?

  • Restore transient fields in readObject(): implement a private readObject(ObjectInputStream in) method to reinitialize transient fields after deserialization.
  • Jackson @JsonIgnore is the JSON equivalent of transient.
  • JPA @Transient is the entity equivalent – marks a field as not persisted to the database.

What are common mistakes?

A common mistake is forgetting to declare serialVersionUID, which causes deserialization failures when the class changes.

Wrong approach:

JAVA
package com.bank.model;

import java.io.Serializable;

// WRONG: no serialVersionUID
public class Account implements Serializable {
    private String accountId;
    private java.math.BigDecimal balance;
}
// If you add a field later, old serialized data cannot be read

Correct approach:

JAVA
package com.bank.model;

import java.io.Serializable;
import java.math.BigDecimal;

// CORRECT: explicit serialVersionUID
public class Account implements Serializable {
    private static final long serialVersionUID = 1L;
    private String accountId;
    private BigDecimal balance;
}
// Adding new fields with defaults still allows reading old data

Another mistake is serializing sensitive fields like passwords or PINs. Always mark them transient to exclude them from the byte stream.

What are the edge cases?

Edge cases can cause unexpected behavior in banking applications. Here are some to watch for.

JAVA
package com.bank.edge;

public class EdgeCaseDemo {
    public static void main(String[] args) {
        // Edge case 1: null handling
        String accountId = null;
        try {
            System.out.println(accountId.length());
        } catch (NullPointerException e) {
            System.out.println("Null account ID");
        }

        // Edge case 2: empty vs null
        String emptyId = "";
        System.out.println(emptyId.isEmpty());  // true
        System.out.println(emptyId == null);     // false

        // Edge case 3: boundary values
        java.math.BigDecimal zero = java.math.BigDecimal.ZERO;
        java.math.BigDecimal negativeBalance = new java.math.BigDecimal("-0.01");
        System.out.println(zero.compareTo(negativeBalance) > 0);  // true
    }
}
TEXT
Null account ID
true
false
true

Always test with null values, empty strings, boundary values, and negative amounts in banking applications.

What are common interview follow-up questions?

Interviewers often follow up with these related questions:

  • How does this concept apply to a high-throughput banking transaction system?
  • What are the performance implications in a production environment?
  • How would you debug issues related to this concept?
  • What tools would you use to monitor this in production?
  • How does this behave differently in Java 8 vs Java 17+?

When answering follow-up questions, always relate your answer back to practical experience. For example, at National Bank of India, Rajesh debugged a production issue related to this concept by using JFR recordings and heap dumps. Providing concrete examples from banking systems shows the interviewer that you understand both the theory and the practice.

Visual Overview

TEXT
  Serialize:    accountId ✓  balance ✓  password ✗  cachedInterest ✗
  Deserialize:  accountId = restored    password = null (default)

  Use for: passwords, caches, loggers, connections
  Jackson equivalent: @JsonIgnore
  JPA equivalent: @Transient

Key Takeaway

transient excludes a field from serialization. On deserialization, it gets its default value. Use it for passwords, cached data, and non-serializable fields like loggers and connections.

One sentence to remember: transient = not serialized, gets default on deserialization.

---

Share
Use ← → arrow keys to navigate