Explain Externalizable in Java

We have seen an example of Serialization and how it works in our previous article. However there is one more interface provided by the Java API which is “Externalizable“. This interface extends “Serializable” and provides two more methods as shown below :

  1. void readExternal(ObjectInput in)
  2. void writeExternal(ObjectOutput out)

Externalizable interface (from API):

Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances. The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods.
Object Serialization uses the Serializable and Externalizable interfaces. Object persistence mechanisms can use them as well. Each object to be stored is tested for the Externalizable interface. If the object supports Externalizable, the writeExternal method is called. If the object does not support Externalizable and does implement Serializable, the object is saved using ObjectOutputStream.
When an Externalizable object is reconstructed, an instance is created using the public no-arg constructor, then the readExternal method called. Serializable objects are restored by reading them from an ObjectInputStream.
An Externalizable instance can designate a substitution object via the writeReplace and readResolve methods documented in the Serializable interface.

Example :

package com.G2.Serialization;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Demo1 implements Externalizable {
	public Demo1() {
		System.out.println("Demo1 Constructor");
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		System.out.println("Demo1 writeExternal()");
	}

	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		System.out.println("Demo1 readExternal()");
	}
}

class Demo2 implements Externalizable {
	public Demo2() {
		System.out.println("Demo2 Constructor");
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		System.out.println("Demo2 writeExternal()");
	}

	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		System.out.println("Demo2 readExternal()");
	}
}

public class ExternizationDemo {
	// Throw exceptions to console:
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		System.out.println("--- GOing to create objects ---");
		Demo1 b1 = new Demo1();
		Demo2 b2 = new Demo2();
		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Demo1.out"));
		System.out.println("--- Saving objects ---");
		o.writeObject(b1);
		o.writeObject(b2);
		o.close();

		// Get back objects
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("Demo1.out"));
		System.out.println(" --- Recovering b1 --- ");
		b1 = (Demo1) in.readObject();
		System.out.println(" --- Object recovered --- ");
		System.out.println(" --- Recovering b2 --- ");
		b2 = (Demo2)in.readObject();
	}
}

Output :

— GOing to create objects —
Demo1 Constructor
Demo2 Constructor
— Saving objects —
Demo1 writeExternal()
Demo2 writeExternal()
— Recovering b1 —
Demo1 Constructor
Demo1 readExternal()
— Object recovered —
— Recovering b2 —
Demo2 Constructor
Demo2 readExternal()

O/p of same program when class implements serializable interface.

— GOing to create objects —
Demo1 Constructor
Demo2 Constructor
— Saving objects —
— Recovering b1 —
— Object recovered —
— Recovering b2 —

As we can see, in case of serialization object is readed from ObjectInputStream whereas in case externalization, default no args constructor is called (Object is created).

Posted

in

by

Tags:


Related Posts

Comments

5 responses to “Explain Externalizable in Java”

  1. […] further read – Externalization API of Java. Sharevar dzone_url = "https://jitendrazaa.com/blog/java/serialization-marshalling-deflating-in-java/"; var dzone_title = "Serialization / Marshalling / Deflating in JAVA"; If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader. […]

  2. […] the flexibility and control over how the object is serialized and deserialized. for further read refer this article. Sharevar dzone_url = "https://jitendrazaa.com/blog/java/java-j2ee-interview-questions-1/"; var […]

  3. Rabindra Singh Avatar
    Rabindra Singh

    Hi,
    I am highly impressed with this blog in serialization. It provides us a good amount of information about this topic.

    Rabindra Singh

    1. Amar Tanwar Avatar
      Amar Tanwar

      This is incorrect…

      if you read an object like as in serializable, you will get optiondataexception.

      Tanwar.
      tanwar.amar@gmail.com

  4. sandeepbh Avatar
    sandeepbh

    you have shared quite good points. Here is one more differentiator article for fellow developers to read on serializable vs externalizable

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading