Home ProgrammingJava Externalization in Java Explained

Externalization in Java Explained

by Log Raj Bhatt
0 comment 1035 views

In the previous post we have discussed about serialization and have written a demo program. In serialization everything is managed by JVM and programer has least control over serialization process. And another problem is we only can store entire object in file using serialization. So whenever we need to serialize some part(few properties only) of object then we use Externalization. This provides programmer more flexibility to control serialization process.

Only the objects of classes which implements Externalizable interface can be externalized.  Externalizable is child interface of the serialization and introduced in 1.1 version with Serializable interface. Mainly Externalizable interface contain two methods that we need to implement in externalizable classes. these methods are

1. void writeExternal():

This method is called automatically at the time of externalization. This method contain code to store properties as per requirement of the programmer. Signature look like this:

public void writeExternal(ObjectOutput o) throws IOException

2. void readExternal():

This method is called automatically at the time of deserialization. Here first JVM will create an object of the externalizable class using no argument constructor of that class. So you must have a public no-argument constructor in the Externalizable class. Then JVM calls readExternal method using that object. As a result that object gets the properties that have been serialized. Signature look like this:

public void readExternal(ObjectInput i) throws IOException          

Here is Demo program, if you once run and understand this program you will be able to differentiate Externalization from Serialization.

import java.io.*;
class PartialSerialize implements Externalizable{
 int sn;
 String name,address;
  public PartialSerialize(){
  System.out.println("This is no argument constructor");
  public PartialSerialize(int i, String n, String a){
  public void writeExternal(ObjectOutput oo) throws IOException{
  oo.writeObject(name); // here we dont want to store address property
  System.out.println("This is writeExternal method");
  public void readExternal(ObjectInput oi) throws IOException,ClassNotFoundException{
	name=(String) oi.readObject();
	System.out.println("This is readExternal method");
class TryIt{
	public static void main(String args[])throws IOException,ClassNotFoundException{
	 FileOutputStream fos=new FileOutputStream("file.txt");
	 ObjectOutputStream oos=new ObjectOutputStream(fos);
	 PartialSerialize ps= new PartialSerialize(34,"LogRaj","Far-Western Region");
	 FileInputStream fis=new FileInputStream("file.txt");
	 ObjectInputStream ois=new ObjectInputStream(fis);
	 PartialSerialize ps1=(PartialSerialize)ois.readObject();


Lets explain this code. The class named PartialSerialize implements Externalizable interface so this class is externalizable class. This class must contain no argument public constructor and we have that. Here two methods writeExternal() and readExternal() must be implemented. I want to save only name and sn property and ignore address property for performance or some other reason. So writeExternal() method contains code for writing only sn and name property. Lets run and analyze output.


Here first line in the output screen is printed at the time of serialization. From this demo you can understand that writeExternal method is called when we serialize  an externalizable object. Similarly at the time of serialization, an object of externalizable class is created by invoking no argument constructor. And then readExternal method is called which contain code to deserialize data. At last remember that Externalizable interface is child of Serializable interface. It is used when you want to serialize only few properties of the object. Now you got Externalization. Comment in case of any doubt and confusion.

0 comment

Related Articles

Leave a Reply

DotNet Guide