Serialization in Java for Beginners

Process of storing the state of object as it is in Java is called Serialization. The interface achieves Serialization in Java. The following tutorial explains Serialization in easy way. Let's kickstart.

Understand Better

Storing objects as it is means, storing the values of data member variables (global variables) as it is. Consider a simple class.
class emp
public int eno;
emp e=new emp();

Now, the value in eno sets to 101 after the statement e.eno=101 is executed. Now storing the value 101 in a file can be done by making class emp serializable using Serializable interface. The following example achieves this.

Real Time

If you can observe, software save user's settings i.e. when you check or un check a check box, that will be saved . How this could be possible? They either store it in the Registry or in a local file which may be of name settings.dat or something of their wish. So, in order to achieve this, Serialization is used. In fact, in my product Booster+ too, i've used the same technique. interface

public interface {

An Example on Serialization

  1. Create Serializable class (should implement Serializable)
  2. Create & store it's object.
class Employee implements Serializable
public int eno;
public String ename;
public double sal;
class WriteSObject
public static void main(String args[]) throws Exception
Employee e=new Employee();
e.ename="Gowtham Gutha";

FileOutputStream fout=new FileOutputStream("out.dat");
ObjectOutputStream out=new ObjectOutputStream(fout);





1. FileOutputStream points out file where data has be written.

2. ObjectOutputStream points FileOutputStream.

Employee e=new Employee(): Create Employee class object.

e.eno=101;e.ename="Gowtham Gutha";e.sal=200000.95; 
Put values in data member variables of employee class.

out.writeObject(e): Write object 'e' in out.dat

out.close(): Close object output stream.

3. Now, read object & print using System.out.println().

class ReadSObject
public static void main(String args[]) throws Exception
FileInputStream fin=new FileInputStream("out.dat");
ObjectInputStream oin=new ObjectInputStream(fin);

Employee e=(Employee) oin.readObject();

System.out.println("Eno : "+e.eno);
System.out.println("Ename : "+e.ename);
System.out.println("Sal : "+e.sal);
Notes: 1. FileInputStream points out.dat (was previously written using FileOutputStream & ObjectOutputStream. See above)
2. ObjectInputStream points FileInputStream.

Employee e= (Employee) oin.readObject(): Read object from out.dat & store in e

3. Print them.

More: All wrapper classes (String, Integer, Double etc) are serializable.

Transient keyword

You know the meaning of transient? Well, it means pass away. So, who has to pass away? The variables, yes the variables that we don't need to be written into the file. How to do that? Simple, just place the transient keyword before the variable at declaration. That's it. Here is an example on transient keyword.

More examples on serialization

Here are two dead simple examples that could ever be written on Serialization. Unlike the above examples, they are simply about String objects.
  1. Read string from a file using ObjectInputStream
  2. Write string to a file using ObjectOutputStream

No comments: