Externalization in Java Explained

1.4K views 3 minutes read
A+A-
Reset

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():

You Might Be Interested In

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){
	sn=i;
	name=n;
	address=a;
  }
  public void writeExternal(ObjectOutput oo) throws IOException{
  oo.writeInt(sn);
  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{
	sn=oi.readInt();
	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");
	 oos.writeObject(ps);
	 
	 FileInputStream fis=new FileInputStream("file.txt");
	 ObjectInputStream ois=new ObjectInputStream(fis);
	 PartialSerialize ps1=(PartialSerialize)ois.readObject();
	 System.out.println(ps1.sn+"...."+ps1.name+"...."+ps1.address);
	}

}

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.

Externalization-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.

Related Posts

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.