Java Externalizable example

17 January 2013
By Gonçalo Marques
In this tutorial you will learn how to implement a custom serialization mechanism by implementing the Externalizable interface.


The Externalizable interface provides the necessary means for implementing a custom serialization mechanism. Implementing the Externalizable interface means that we must override some of its methods, namely the writeExternal and readExternal methods. These methods will be called when you serialize (or deserialize) a given instance. If you know about the Serializable interface you may have already noticed that there are some differences. When we implement the Serializable interface we don't need to implement any method: The serialization takes place automatically. Even if we want to implement our own serialization mechanism by implementing the Serializable interface (yes it is also possible by defining writeObject and readObject methods) we don't need to override or implement any method. The JVM calls the serialization methods from our class using reflection. In early JVM implementations reflection performance was kind of slow so the Externalizable interface was also used to overcome this disadvantage.


  1. Ubuntu 12.04
  2. JDK

A simple class

Let's define a simple class to use in this example:

User class implementing the Externalizable interface
package com.byteslounge.serialization;


public class User implements Externalizable {
  private int id;
  private String username;
  private String email;
  public User(int id, String username, String email) { = id;
    this.username = username; = email;
  public User(){

  public int getId() {
    return id;

  public String getUsername() {
    return username;

  public String getEmail() {
    return email;

  public void writeExternal(ObjectOutput out) throws IOException {

  public void readExternal(ObjectInput in) throws IOException,
      ClassNotFoundException {
    id = in.readInt();
    username = (String) in.readObject();
    email = (String) in.readObject();

Note that we are implementing the Externalizable interface and overriding writeExternal and readExternal methods. We must also define a default constructor when we implement the Externalizable interface. It we fail to do so the serialization will fail.


Let's create a simple test class:

package com.byteslounge.serialization;


public class Main {

  public static void main(String[] args) 
    throws Exception {
    User userWrite = new User(1, "johndoe", "John Doe");
    FileOutputStream fos = new FileOutputStream("testfile");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    User userRead;
    FileInputStream fis = new FileInputStream("testfile");
    ObjectInputStream ois = new ObjectInputStream(fis);
    userRead = (User)ois.readObject();
    System.out.println("username: " + userRead.getUsername());



When we run the test the following output is generated:

username: johndoe

The example source code is available at the end of this page.

Download source code from this article

Related Articles


About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile:

He is also the author of the WiFi File Browser Android application: