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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
 }
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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();
     }
 
    }
   }
  }
 }
 
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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();
    }
   }
  }
 
 }
 
}
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
2
3
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
 }
 
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
 }
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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();
     }
 
    }
   }
  }
 }
 
}
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

Karibasappa G C (KB)
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