Topics Index

Externalizable Test - Java Example Program

Externalizable Test

class ExternalizableTest
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        Student hemanth = new Student("Hemanth", 25, 'C');
        System.out.println("Before - " + + " " + hemanth.rollNumber + " " + hemanth.section);
        oos.writeObject(hemanth); // LINE A
        byte[] objectDataBytes = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(objectDataBytes);
        ObjectInputStream ois = new ObjectInputStream(bais);
        Student hemanthRestored = (Student) ois.readObject(); // LINE B
        System.out.println("After - " + + " " + hemanthRestored.rollNumber + " " + hemanthRestored.section);

class Student implements Externalizable {

    String name = "";
    int rollNumber = 0;
    char section = 'A';

    public Student() { }

    public Student(String name, int rollNumber, char section) {
        super(); = name;
        this.rollNumber = rollNumber;
        this.section = section;

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        name = in.readUTF();
        rollNumber = in.readInt();
        // section = in.readChar(); // LINE X

    public void writeExternal(ObjectOutput out) throws IOException {
        // out.writeChar(section); // LINE Y

Before - Hemanth 25 C
After - Hemanth 25 A


Here we are creating an ObjectOutputStream using an ByteArrayOutputStream. Then we are serializing the created hemanth object into the ObjectOutputStream at LINE A. By doing this the object data, basically "Hemanth", 25 and C are converted into bytes and then written into the output stream.
Later the converted bytes data is given as input to ByteArrayInputStream which in-turn is input to ObjectInputStream. At LINE B, using the ObjectInputStream the object data is read back or de-serialized. Since we know the read object is Student, we are type casting it to that class.
As you can see the data before serialization and the after serialization is note same. This is because in the readExternal and writeExternal methods we are not taking care of section. That is why the value of section after de-serializing is set to the default value of 'A'. This way you can serialize only the member variables you want to serialize and ignore the rest. You can also change how the data is stored.

  • Uncomment LINE X and LINE Y and see that the section is also retained after the serialization and the value 'C' is printed.
  • Add one more variable percentage of type double to the Student class and initialize it 75.6. Change before and after println statements and see that the value 75.6 is not retained after the de-serialization process. This is because we have not added the code for writing and reading the percentage variable. Change the methods readExternal and writeExternal to handle percentage variable. Please use the methods writeDouble and readDouble for the same.
  • Remove the default constructor CONSTRUCTOR 1 and see that the readObject fails with InvalidClassException
  • Remove the type cast to Student at LINE B and see that we get a compilation error since readObject returns an Object and not Student.
  • Change the order of the method calls in readExternal and writeExternal and see that if we do not use the same order in both the methods, then we might get inconsistent data or run-time errors.

© meritcampus 2019

All Rights Reserved.

Open In App