Java Object Streams

Previous
Next

Object streams (Serialization)

  • Writing(Storing) Object information in a physical storage area(File, Database ..)
  • Serialization is the concept of converting Object state into Persistent(permanent) state.
  • Generally in Java application, objects will be created in Heap area and will be deleted as soon as execution completes.
  • For every application it is mandatory to maintain the data permanently.
  • Java API providing classes and interfaces to perform serialization.
    • java.io.Serializable(interface)
    • java.io.ObjectInputStream(class)
    • java.io.ObjectOutputStream(class)

Note :

  • To serialize an Object, the class must implements Serializable interface.
  • In the process of serialization, transient variables will not participate.
  • Serializable information must be stored into a file with .ser extension.
  • Serializable file will be in binary format, hence we can transfer the file over the network.
  • In the network, receiver should de-serialize the file to check the information.

Serialization : Converting Object —> File

De-Serialization : Converting File –> Object

The following example code shows how to serialize and de-serialize of an object, it includes three programs,

  1. Defining a class that includes some properties of an employee.
  2. Serializing Employee object.
  3. De-serializing Employee object.

Employee.java:

public class Employee implements java.io.Serializable{
	private int eno ;
	private String ename ;
	private double esal ;
	private transient int SSN ;
	public Employee()
	{
	}
	public Employee(int eno, String ename, double esal, int SSN){
		this.eno = eno ;
		this.ename = ename ;
		this.esal = esal ;
		this.SSN = SSN ;
	}
	public int getEno(){
		return this.eno;
	}
	public void setEno(int eno){
		this.eno = eno ;
	}
	public String getEname(){
		return this.ename;
	}
	public void setEname(String ename){
		this.ename = ename ;
	}
	public double getEsal(){
		return this.esal;
	}
	public void setEsal(double esal){
		this.esal = esal ;
	}
	public int getSSN(){
		return this.SSN;
	}
	public void setSSN(int SSN){
		this.SSN = SSN ;
	}
}

Serialization.java:

import java.io.*;
public class Serialization {
	public static void main(String[] args) throws Exception{
		FileOutputStream fos = null ;
		ObjectOutputStream oos = null ;
		try{
			fos = new FileOutputStream("g:/Empl.ser");
			oos = new ObjectOutputStream(fos);
			Employee emp = new Employee(101 , "Syam" , 30000 , 12345);
			oos.writeObject((Object)emp);
			System.out.println("Object serialized successfully.....");
		}
		finally{
			if(oos != null){
				oos.close();
			}
			if(fos != null){
				fos.close();
			}
		}
	}
}

DeSerialization.java:

import java.io.*;
public class DeSerialization {
	public static void main(String[] args) throws Exception{
		FileInputStream fis = null ;
		ObjectInputStream ois = null ;
		try{
			fis = new FileInputStream("g:/Empl.ser");
			ois = new ObjectInputStream(fis);
			Employee emp = (Employee)ois.readObject();
			System.out.println("Object De-serialized successfully.....");
			System.out.println("Ename : "+emp.getEname());
			System.out.println("SSN : "+emp.getSSN());
		}
		finally{
			if(ois != null){
				ois.close();
			}
			if(fis != null){
				fis.close();
			}
		}
	}
}

transient keyword:

  • The keyword transient in Java used to indicate that the variable should not be serialized.
  • By default all the variables in the object convert into persistent state.
  • In some cases, you may want to avoid some variables (confidential data) because you don’t have the necessity to transfer across the network.
  • So, you can declare those variables as transient. If the variable is declared as transient, then it will not be persisted means will not be participated in Serialization process.

How many methods Serializable has? If not, then what is the purpose of Serializable interface?

  • Serializable interface doesn’t have any method. It is empty interface. It is also called Marker Interface or Tagged interface in Java.
  • Marker interface is used as a information tag about an object.
  • It adds special behavior to Object of class which is implementing it.
  • Examples
    • java.io.Serializable interface
    • java.lang.Cloneable interface

Can we Serialize an object which is pointing to a non-serializable object?

  • One class(implements Serializable) pointing to(has-A relation with) another object which is not Serializable.
  • If we try to serialize the object results Exception.
import java.io.*;
class Mobile // implements Serializable{
	String color ;
	String model ;
	Mobile(String color, String model){
		this.color = color ;
		this.model = model ;
	}
	void call(){
		System.out.println("Calling.....");
	}
	void message(){
		System.out.println("Texting....");
	}
}
class Human implements Serializable{
	Mobile mob ; // Has - A relation
	Human(Mobile mob){
		this.mob = mob ;
	}
	void operateMobile(){	}
}
class Serialize_Has_A{
	public static void main(String args[ ]) throws Exception{
		Mobile mob = new Mobile("black" , "nokia");
		Human hum = new Human(mob);
		FileOutputStream fos = null ;
		ObjectOutputStream oos = null ;
		try{
			fos = new FileOutputStream("g:/Has_A.ser");
			oos = new ObjectOutputStream(fos);
			oos.writeObject((Object)hum);
			System.out.println("Object serialized successfully.....");
		}
		catch(NotSerializableException nse){
			System.out.println("Class couldn't serialize : "+nse.getMessage());
		}
		finally{
			if(oos != null){
				oos.close();
			}
			if(fos != null){
				fos.close();
			}
		}
	}
} 

Which kind of variables is not serialized during Java Serialization?

  • Static and Transient variables.
  • Since static variables belong to the class and not to an object they are not the part of the state of object so they are not saved during Java Serialization process.
  • As Java Serialization only persist state of object and not object itself.
  • Transient variables are also not included in java serialization process and are not the part of the object’s serialized state.

POJO v/s Bean class:

            The only difference between POJO & Bean class is, POJO class doesn’t implements Serializable interface where as Bean class does it.

public class POJO{
	// rules of POJO
}
public class Bean implements java.io.Serializable{
	// rules of POJO
}
Previous
Next

Add Comment

Courses Enquiry Form