Home >> Java >> Externalization in Java Explained
Externalization in java

Externalization in Java Explained

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.

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.

About Log Raj Bhatt

Check Also


Serialization in Java

Serialization in java is just a process of converting objects into sequence of bytes due …

Leave a Reply

Your email address will not be published. Required fields are marked *