Table of contents
1.
Introduction
2.
What are Serialization and Externalization?
2.1.
Serialization
2.1.1.
Example:
2.2.
Externalization
3.
Key Difference
4.
Externalization in Java: When Do You Use It?
5.
The Externalizable Interface
5.1.
Syntax
6.
Externalizable Interface Methods: An Overview
6.1.
writeExternal(ObjectOutput out)
6.2.
readExternal(ObjectInput in)
7.
Features of Externalization
8.
Example of Externalization in Java
9.
Frequently Asked Questions
9.1.
How is Externalization different from Serialization?
9.2.
Why use Externalization instead of Serialization?
9.3.
What happens if we don't implement writeExternal() and readExternal()?
10.
Conclusion
Last Updated: Mar 11, 2025
Medium

What Is Externalization In Java?

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Externalization in Java is a mechanism that allows developers to have complete control over the serialization process. It is implemented using the Externalizable interface, which requires defining the writeExternal() and readExternal() methods. Unlike default serialization, externalization enables selective serialization of object fields, improving efficiency. This approach is useful for optimizing performance in applications dealing with large objects. 

What Is Externalization In Java?

In this article, we will discuss externalization, its working, advantages, and implementation with examples.

What are Serialization and Externalization?

Serialization

Serialization is the process of converting an object into a byte stream so it can be saved or transmitted. Java provides the Serializable interface, which allows objects to be serialized automatically.

Example:

import java.io.*;
class Student implements Serializable {
    int id;
    String name;
    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

Here, the Student class implements Serializable, allowing it to be converted into a byte stream without extra methods.

Externalization

Externalization is an advanced version of Serialization where the developer defines how an object is written and read. This is done using the Externalizable interface.

Key Difference

 Unlike Serializable, which handles everything automatically, Externalizable requires implementing two methods: writeExternal() and readExternal().

Difference Between Serialization and Externalization in Java

ParametersSerializationExternalization
InterfaceImplements SerializableImplements Externalizable
ControlJava manages serializationDeveloper defines how to serialize and deserialize objects
PerformanceCan be slow due to default behaviorFaster if optimized correctly
MethodsNo methods requiredRequires writeExternal() and readExternal()
CustomizationLimited customizationComplete control over serialization process

Externalization in Java: When Do You Use It?

Externalization is useful when:

  1. You need more control over serialization. Unlike default serialization, it allows selective storage of object properties.
     
  2. Performance is a concern. Since developers define what to save, it can be optimized to store only necessary data.
     
  3. Objects contain sensitive data. Custom serialization helps in excluding certain fields for security reasons.

The Externalizable Interface

The Externalizable interface is part of java.io and extends Serializable. It forces developers to implement:

  • writeExternal(ObjectOutput out): Defines how an object is written.
     
  • readExternal(ObjectInput in): Defines how an object is read.

Syntax

import java.io.*;

class MyClass implements Externalizable {
    public void writeExternal(ObjectOutput out) throws IOException {
        // Custom serialization logic
    }
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        // Custom deserialization logic
    }
}

Externalizable Interface Methods: An Overview

writeExternal(ObjectOutput out)

  • This method is used to write an object manually.
     
  • Developers decide which fields should be serialized.
     
  • Data is stored in an optimized format.

readExternal(ObjectInput in)

  • Reads back the serialized object.
     
  • Developers manually extract data and restore object properties.

Features of Externalization

  1. Full Control: The developer decides which fields are serialized.
     
  2. Efficient Storage: Reduces unnecessary data storage, optimizing performance.
     
  3. Security: Sensitive fields can be excluded from serialization.
     
  4. Backward Compatibility: Externalization allows modifications without affecting existing serialized objects.

Example of Externalization in Java

Let's see an example where we use Externalization to store and retrieve an object.

import java.io.*;
class Student implements Externalizable {
    private int id;
    private String name;
    
    public Student() {}
    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeInt(id);
        out.writeObject(name);
    }
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        id = in.readInt();
        name = (String) in.readObject();
    }
    public void display() {
        System.out.println("ID: " + id + ", Name: " + name);
    }
}
public class ExternalizationExample {
    public static void main(String[] args) {
        try {
            Student student = new Student(101, "John");
            
            // Serialize
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.txt"));
            oos.writeObject(student);
            oos.close();
            // Deserialize
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("student.txt"));
            Student newStudent = (Student) ois.readObject();
            ois.close();
            // Display Deserialized Object
            newStudent.display();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

ID: 101, Name: John


Explanation:

  • We define a Student class implementing Externalizable.
     
  • writeExternal() manually writes id and name to a file.
     
  • readExternal() reads back the data.
     
  • The main() method serializes and deserializes an object, demonstrating how Externalization works.

Frequently Asked Questions

How is Externalization different from Serialization?

Externalization allows manual control over object serialization, whereas Serialization automatically handles it.

Why use Externalization instead of Serialization?

Externalization improves performance by letting developers store only necessary data, reducing overhead.

What happens if we don't implement writeExternal() and readExternal()?

Since Externalizable requires these methods, failing to implement them will result in an error.

Conclusion

In this article, we discussed Externalization in Java, which is an advanced serialization mechanism that allows developers to have full control over object serialization. By implementing the Externalizable interface, we can customize how objects are written and read, making the process more efficient and flexible. This is useful for optimizing performance and reducing serialization size.

Live masterclass