• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Externalizable Interface in Java

August 21, 2007 //  by Krishna Srinivasan//  Leave a Comment

Serialization is the process of giving persistence storage to Java objects so that they can be restored at a later time. Classes can be made persistent in Java by implementing the Serializable interface. Serializable is a marker interface meaning that it has no methods within it. Any Serializable class can be passed on to ObjectOutputStream.writeObject(object) for saving the object state and a call back to ObjectInputStream.readObject() will get the object back from the storage.

also read:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials
  • What is transient keyword?

Since this is a default Serialization process, the Application cannot have a finer control over the Serialization process. Because, by default all the instance variables, except static and transient variables will undergo Serialization process.

Let us see in this article how to take full control over the Serialization process by depending on the Externalizable interface. This interface has two methods called writeExternal() and readExternal() that needs to be overridden for making up the Serialization process.
Let us say that we wish to make the following Movie class to implement the Externalizable interface for custom Serialization. Note that in writeExternal() method, we store all the fields to the Output Stream by calling the ObjectOutput.write###() methods. And during the restoration process, readExternal() will be called, where the implementation stores the appropriate values in their corresponding fields.
Movie.java

package tips.externaizable;
    public class Movie implements Externalizable{
        private String name;
        private long releaseYear;
        private String language;
        public Movie(){
        }
        public Movie(String name, long releaseYear, String language){
            this.name = name;
            this.releaseYear = releaseYear;
            this.language = language;
        }
        public String toString(){
            return name + " , " + releaseYear + " , " + language;
        }
        @Override
        public void readExternal(ObjectInput in)
        throws IOException,ClassNotFoundException {
            name = in.readUTF();
            releaseYear = in.readLong();
            language = in.readUTF();
        }
        @Override
        public void writeExternal(ObjectOutput out)
        throws IOException {
            out.writeUTF(name);
            out.writeLong(releaseYear);
            out.writeUTF(language);
        }
    }

Following is the Client program that makes use of the above Movie class. The Object Output Stream class comes into picture for saving the object state in a file called "movies". For retrieving the object back from the storage we use the Object Input Stream class.
MovieExternalizableTest.java

  package tips.externalizable;
    import java.io.*;
    public class MovieExternalizableTest {
        public static void main(String[] args) throws Exception{
            Movie harry = new Movie(
                "Harry Potter and the Order of Phoenix", 2007, "English");
            ObjectOutputStream output = new ObjectOutputStream(
                new FileOutputStream("movies"));
            output.writeObject(harry);
            output.close();
            ObjectInputStream input = new ObjectInputStream(
                new FileInputStream("movies"));
            harry = (Movie)input.readObject();
            System.out.println(harry);
            input.close();
        }
    }

Category: JavaTag: Core Java

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Enhanced for-loop for User-defined objects
Next Post: Overriding the toString() method in Object class »

Reader Interactions

Leave a Reply Cancel reply

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

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

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact