Serialization and externalization in java

Serialization is the process of writing an object state into the file.

state of an object means atrributes and its values and in future if we need that object we can rebuild it from the file and  we call that process as “de serialization”

so to achive serialization in java,  any class must implement one marker interface.

that marker interface is “serializable”.

lets understand this process through below example

  1. package com.kb.model;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class Employee implements Serializable{
  6.  
  7.  private String name;
  8.  private int age;
  9.  private String designation;
  10.  public String getName() {
  11.   return name;
  12.  }
  13.  public void setName(String name) {
  14.   this.name = name;
  15.  }
  16.  public String getDesignation() {
  17.   return designation;
  18.  }
  19.  public void setDesignation(String designation) {
  20.   this.designation = designation;
  21.  }
  22.  public int getAge() {
  23.   return age;
  24.  }
  25.  public void setAge(int age) {
  26.   this.age = age;
  27.  }
  28. }
package com.kb.model;

import java.io.Serializable;

public class Employee implements Serializable{

 private String name;
 private int age;
 private String designation;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getDesignation() {
  return designation;
 }
 public void setDesignation(String designation) {
  this.designation = designation;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
}
  1. package com.kb.client;
  2.  
  3. import java.io.File;
  4.  
  5. public class Serialize {
  6.  
  7.  public static void main(String[] args) {
  8.   Employee emp = new Employee();
  9.   emp.setAge(25);
  10.   emp.setDesignation("se");
  11.   emp.setName("kb");
  12.   File file = new File("employee.ser");
  13.   FileOutputStream fout = null;
  14.   ObjectOutputStream oout = null;
  15.   try {
  16.    fout = new FileOutputStream(file);
  17.    oout = new ObjectOutputStream(fout);
  18.    oout.writeObject(emp);
  19.   } catch (IOException e) {
  20.   } catch (Exception e) {
  21.  
  22.   }
  23.  
  24.   finally {
  25.    if (oout != null) {
  26.     try {
  27.      oout.flush();
  28.      oout.close();
  29.      oout = null;
  30.     } catch (IOException e) {
  31.      e.printStackTrace();
  32.     }
  33.     if (fout != null) {
  34.      try {
  35.       fout.close();
  36.       fout = null;
  37.      } catch (IOException e) {
  38.       e.printStackTrace();
  39.      }
  40.  
  41.     }
  42.    }
  43.   }
  44.  }
  45.  
  46. }
package com.kb.client;

import java.io.File;

public class Serialize {

 public static void main(String[] args) {
  Employee emp = new Employee();
  emp.setAge(25);
  emp.setDesignation("se");
  emp.setName("kb");
  File file = new File("employee.ser");
  FileOutputStream fout = null;
  ObjectOutputStream oout = null;
  try {
   fout = new FileOutputStream(file);
   oout = new ObjectOutputStream(fout);
   oout.writeObject(emp);
  } catch (IOException e) {
  } catch (Exception e) {

  }

  finally {
   if (oout != null) {
    try {
     oout.flush();
     oout.close();
     oout = null;
    } catch (IOException e) {
     e.printStackTrace();
    }
    if (fout != null) {
     try {
      fout.close();
      fout = null;
     } catch (IOException e) {
      e.printStackTrace();
     }

    }
   }
  }
 }

}

when we execute the above program, one file gets created in our project names as “employee.ser”. this file contains the information about the employee object which we have written into this file.

this process is called serialization.

Now lets see de-serialization.

its just reading a serialized object from the file into the object back.

means retaining the object state.

lets do it as below

  1. package com.kb.client;
  2.  
  3. import java.io.File;
  4.  
  5. public class DeSerialize {
  6.  
  7.  public static void main(String[] args) {
  8.   File file = new File("employee.ser");
  9.  
  10.   FileInputStream fin = null;
  11.   ObjectInputStream oin = null;
  12.  
  13.   try {
  14.    fin = new FileInputStream(file);
  15.    oin = new ObjectInputStream(fin);
  16.    Employee emp = (Employee) oin.readObject();
  17.    System.out.println(emp.getAge());
  18.    System.out.println(emp.getDesignation());
  19.    System.out.println(emp.getName());
  20.   } catch (Exception e) {
  21.    // TODO: handle exception
  22.   }
  23.  
  24.   finally {
  25.    if (oin != null) {
  26.     try {
  27.      oin.close();
  28.      oin = null;
  29.     } catch (IOException e) {
  30.      e.printStackTrace();
  31.     }
  32.     ;
  33.  
  34.    }
  35.  
  36.    if (fin != null) {
  37.     try {
  38.      fin.close();
  39.      fin = null;
  40.     } catch (Exception e) {
  41.      e.printStackTrace();
  42.     }
  43.    }
  44.   }
  45.  
  46.  }
  47.  
  48. }
package com.kb.client;

import java.io.File;

public class DeSerialize {

 public static void main(String[] args) {
  File file = new File("employee.ser");

  FileInputStream fin = null;
  ObjectInputStream oin = null;

  try {
   fin = new FileInputStream(file);
   oin = new ObjectInputStream(fin);
   Employee emp = (Employee) oin.readObject();
   System.out.println(emp.getAge());
   System.out.println(emp.getDesignation());
   System.out.println(emp.getName());
  } catch (Exception e) {
   // TODO: handle exception
  }

  finally {
   if (oin != null) {
    try {
     oin.close();
     oin = null;
    } catch (IOException e) {
     e.printStackTrace();
    }
    ;

   }

   if (fin != null) {
    try {
     fin.close();
     fin = null;
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  }

 }

}

now run the above program and see the below output

  1. 25
  2. se
  3. kb
25
se
kb

Remember in serialization

1)If we dont want to save any of the attribute from the serializable class then we can make such attributes as “transient”.

2)all static fields can not be serializable sine its a class attribute not a object attribute.

in the above example if we dont want to save attribute designation then make it as transient as below

transient String designation;

then run both the program, in the output we can see “null ” for that designation in the de-serializable program output.

now what if the employee class has address class reference and if address class is not serialized then while serializing employee object we will get an exception

scenario explained above through program is as below

address class – not implemented serializable interface.

  1. package com.kb.model;
  2.  
  3. public class Address {
  4.  private String street;
  5.  private String location;
  6.  public String getStreet() {
  7.   return street;
  8.  }
  9.  public void setStreet(String street) {
  10.   this.street = street;
  11.  }
  12.  public String getLocation() {
  13.   return location;
  14.  }
  15.  public void setLocation(String location) {
  16.   this.location = location;
  17.  }
  18.  
  19. }
package com.kb.model;

public class Address {
 private String street;
 private String location;
 public String getStreet() {
  return street;
 }
 public void setStreet(String street) {
  this.street = street;
 }
 public String getLocation() {
  return location;
 }
 public void setLocation(String location) {
  this.location = location;
 }

}

now consider below employee class

  1. package com.kb.model;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class Employee implements Serializable{
  6.  
  7.  private String name;
  8.  private int age;
  9.  private transient String designation;
  10.  private Address address;
  11.  public String getName() {
  12.   return name;
  13.  }
  14.  public void setName(String name) {
  15.   this.name = name;
  16.  }
  17.  public String getDesignation() {
  18.   return designation;
  19.  }
  20.  public void setDesignation(String designation) {
  21.   this.designation = designation;
  22.  }
  23.  public int getAge() {
  24.   return age;
  25.  }
  26.  public void setAge(int age) {
  27.   this.age = age;
  28.  }
  29.  public Address getAddress() {
  30.   return address;
  31.  }
  32.  public void setAddress(Address address) {
  33.   this.address = address;
  34.  }
  35. }
package com.kb.model;

import java.io.Serializable;

public class Employee implements Serializable{

 private String name;
 private int age;
 private transient String designation;
 private Address address;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getDesignation() {
  return designation;
 }
 public void setDesignation(String designation) {
  this.designation = designation;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public Address getAddress() {
  return address;
 }
 public void setAddress(Address address) {
  this.address = address;
 }
}

now see the below client program to serialize

  1. package com.kb.client;
  2.  
  3. import java.io.File;
  4.  
  5. public class Serialize {
  6.  
  7.  public static void main(String[] args) {
  8.  
  9.   Employee emp = new Employee();
  10.   emp.setAge(25);
  11.   emp.setDesignation("se");
  12.   emp.setName("kb");
  13.   Address address = new Address();
  14.   address.setLocation("bang");
  15.   address.setStreet("rajajinagar");
  16.   emp.setAddress(address);
  17.   File file = new File("employee.ser");
  18.   FileOutputStream fout = null;
  19.   ObjectOutputStream oout = null;
  20.   try {
  21.    fout = new FileOutputStream(file);
  22.    oout = new ObjectOutputStream(fout);
  23.    oout.writeObject(emp);
  24.    System.out.println("done");
  25.   } catch (IOException e) {
  26.   } catch (Exception e) {
  27.  
  28.   }
  29.  
  30.   finally {
  31.    if (oout != null) {
  32.     try {
  33.      oout.flush();
  34.      oout.close();
  35.      oout = null;
  36.     } catch (IOException e) {
  37.      e.printStackTrace();
  38.     }
  39.     if (fout != null) {
  40.      try {
  41.       fout.close();
  42.       fout = null;
  43.      } catch (IOException e) {
  44.       e.printStackTrace();
  45.      }
  46.  
  47.     }
  48.    }
  49.   }
  50.  }
  51.  
  52. }
package com.kb.client;

import java.io.File;

public class Serialize {

 public static void main(String[] args) {

  Employee emp = new Employee();
  emp.setAge(25);
  emp.setDesignation("se");
  emp.setName("kb");
  Address address = new Address();
  address.setLocation("bang");
  address.setStreet("rajajinagar");
  emp.setAddress(address);
  File file = new File("employee.ser");
  FileOutputStream fout = null;
  ObjectOutputStream oout = null;
  try {
   fout = new FileOutputStream(file);
   oout = new ObjectOutputStream(fout);
   oout.writeObject(emp);
   System.out.println("done");
  } catch (IOException e) {
  } catch (Exception e) {

  }

  finally {
   if (oout != null) {
    try {
     oout.flush();
     oout.close();
     oout = null;
    } catch (IOException e) {
     e.printStackTrace();
    }
    if (fout != null) {
     try {
      fout.close();
      fout = null;
     } catch (IOException e) {
      e.printStackTrace();
     }

    }
   }
  }
 }

}

run this , we will end up with an exception “not serializable exception”

so to serialize any object , if its clas is having any derived type reference like address then those derived types class must also be serialized first otherwise we will end with the above exception.

we will see about externalization and its flexibility and advantages over serialization in next post.

Thanks for reading

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on