Externalization in Java

In Serialization, everything is taken care of by JVM and the programmer does not have any control. In serialization, it is always possible to save the total object to the file, but it is not possible to save part of the object, which may create performance-related problems. To overcome these problems we should go for Externalization. The main advantage of Externalization is everything is taken care of by the programmer and JVM does not have any control. Based on our requirement we can save either the entire object or some part of the object.

To provide Externalizable ability to Java objects it is compulsory that the corresponding Java class should implement Externalizable interface which is part of the java.io package.

Externalizable Method

Externalizable Interface defines two methods-

  • writeExternal()
  • readExternal()

writeExternal

public void writeExternal(ObjectOutput out) throws IOException 

This method will be automatically executed at the time of serialization. In this method, we have to write code to save the required variable to the file.

readExternal

public void readExternal(ObjectInput in) throws IOException

This method will be automatically executed at the time of deserialization. In this method, we have to write code to read required variables from the file.

See Also

Leave a Comment