Back to Serialization & transient
Q11Question 11 of 11

What is the Externalizable interface?

Key Concepts
  • Externalizable gives you full control over what is serialized and how
  • You implement writeExternal() and readExternal() methods
  • Unlike Serializable, Externalizable calls the default no-arg constructor during deserialization
  • You decide exactly which fields to write and read – more efficient, more code
  • Rarely used in modern Java – JSON/Protobuf provide the same control with better tooling

Definition

Externalizable gives full control over serialization format. You implement writeExternal/readExternal. Unlike Serializable, it calls the no-arg constructor during deserialization.

Externalizable vs Serializable:

  • Serializable: JVM handles field serialization automatically. No-arg constructor NOT called.
  • Externalizable: You write writeExternal() and readExternal(). No-arg constructor IS called. Full control over format.

Serializable is auto-pilot photocopying – the machine copies everything. Externalizable is manual photocopying – you choose exactly which pages to copy and in what order.

Understanding the Concepts

Externalizable example

JAVA
public class Account implements Externalizable {
    private String accountId;
    private BigDecimal balance;
    private transient String password;  // not written either way

    public Account() {}  // REQUIRED: no-arg constructor called on deserialization

    public Account(String id, BigDecimal bal, String pw) {
        this.accountId = id; this.balance = bal; this.password = pw;
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(accountId);
        out.writeUTF(balance.toPlainString());
        // password intentionally NOT written
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException {
        this.accountId = in.readUTF();
        this.balance = new BigDecimal(in.readUTF());
        // password remains null
    }
}

Serializable vs Externalizable

| Feature | Serializable | Externalizable |
|---|---|---|
| Field control | Automatic (all non-static, non-transient) | Manual (you choose) |
| No-arg constructor | NOT called | REQUIRED and called |
| Performance | Slightly slower (reflection) | Faster (explicit reads/writes) |
| Effort | Minimal | More code |
| Modern usage | Rare (prefer JSON) | Very rare |

What should a senior engineer know?

  • Externalizable is almost never used in modern Java. JSON serialization (Jackson) provides the same control with annotations.
  • If you need custom serialization with Serializable, implement writeObject/readObject methods instead of switching to Externalizable.

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
  Serializable:    JVM reads/writes all fields automatically
                   Constructor NOT called on deserialize

  Externalizable:  You implement writeExternal/readExternal
                   No-arg constructor IS called on deserialize
                   Full control, more code

  Modern: use Jackson @JsonProperty / @JsonIgnore for fine control

Key Takeaway

Externalizable gives full control over serialization by requiring you to implement writeExternal and readExternal. Unlike Serializable, it calls the no-arg constructor during deserialization. It is more efficient but requires more code. In modern Java, JSON libraries provide the same control with better tooling.

One sentence to remember: Externalizable = full manual control over serialization format, requires no-arg constructor, rarely used in modern code.

---

Share
Use ← → arrow keys to navigate