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
- 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;
- }
- }
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; } }
- 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();
- }
- }
- }
- }
- }
- }
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
- 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();
- }
- }
- }
- }
- }
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
- 25
- se
- 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.
- 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;
- }
- }
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
- 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;
- }
- }
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
- 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();
- }
- }
- }
- }
- }
- }
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
Is Static Variables or Static reference of a class are stored in either Heap area or Method Area?
First thing, static variables will never be a part of method, it will be a part of class always.
So only we cant declare any static variables inside a method but we can access them inside a method.
All static variables will be stored in separate segment inside Heap area only.
Dear Kb,
I go through your blog quite often. Concepts explained out here are very lucid.
Much appreciated .
Thanks a lot Vivek !!
Sure will keep doing my best.
Happy Learning !
Hey Karibasappa,
You are one amazing talented person.
Great examples.
Good job.
Keep it up.
Thank you !!
The blog is very helpful. Thanks a lot for sharing! Also the “Inheritance in interface” page is not opening. Can you please fix that?
Hi Pravakar,
Thank you !!
I am looking into it, will resolve it soon.
Good to inform you that, inheritance in interface link issue is working now !!