What is Externalizable in Java: Explanation and Example
Externalizable is an interface in Java that allows a class to control its own serialization and deserialization process by implementing writeExternal and readExternal methods. It gives you full control over how an object's data is saved and restored, unlike Serializable which uses default behavior.How It Works
Imagine you have a box with items inside, and you want to send it to a friend. Using Serializable is like letting someone pack the box for you automatically, without you choosing what goes in. But with Externalizable, you get to decide exactly what items to pack and how to arrange them.
In Java, when a class implements Externalizable, it must define two methods: writeExternal to specify how to save the object's data, and readExternal to specify how to restore it. This means you control the exact data format and content during serialization and deserialization.
This approach is useful when you want to optimize the data saved, skip some fields, or handle complex data structures manually.
Example
This example shows a simple class implementing Externalizable to save and load its data manually.
import java.io.*; public class Person implements Externalizable { private String name; private int age; // Mandatory public no-arg constructor public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(name); out.writeInt(age); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { name = in.readUTF(); age = in.readInt(); } @Override public String toString() { return "Person{name='" + name + "', age=" + age + "}"; } public static void main(String[] args) throws IOException, ClassNotFoundException { Person p1 = new Person("Alice", 30); // Serialize ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); p1.writeExternal(oos); oos.flush(); byte[] data = baos.toByteArray(); // Deserialize ByteArrayInputStream bais = new ByteArrayInputStream(data); ObjectInputStream ois = new ObjectInputStream(bais); Person p2 = new Person(); p2.readExternal(ois); System.out.println(p2); } }
When to Use
Use Externalizable when you need full control over how your object's data is saved and loaded. This is helpful if you want to:
- Optimize the size of the saved data by excluding unnecessary fields.
- Customize the format for compatibility with other systems.
- Handle complex data structures that default serialization can't manage well.
For example, in network communication or file storage where performance and size matter, Externalizable lets you tailor the process exactly.
Key Points
- Externalizable requires implementing
writeExternalandreadExternalmethods. - It gives you full control over serialization, unlike
Serializablewhich is automatic. - A public no-argument constructor is mandatory for deserialization.
- Useful for optimizing data size and format.
- More complex to implement but more flexible.