Serialization in java

Serialization in java means writing a state of the object to the stream. In this section you will learn about how to serialize and deserialize the object.

Serialization in java

Serialization in java means writing a state of the object to the stream. In this section you will learn about how to serialize and deserialize the object.

Serialization in java

Serialization in java

In this section you will learn about serialization in java. Serialization is the process of writing the state of the object to the stream, a object can be represented as sequence of byte. While writing the state of object to the stream it includes object type as well as type of data stored in the object. A class can be called serialized only when its implements java.io.Serializable interface and classes which is not serialized then any of their state is not serialized. After the object is written to the file it can be read from the file. ObjectInputStream and ObjectOutputStream classes contains method for serialization and desalinization of object.

The ObjectOutputStream and ObjectInputStream class contains write method and read method for writing and reading the object state :

  • public void writeObject(Object obj) throws IOException : This method write the object state into the stream. Likewise ObjecctInputStream contain method for deserialization of object.
  • public void readObject(Object obj)throws IOException, ClassNotFoundException : The readObject method is responsible for reading object state from the stream

When you want to serialize an object then you need to implements marker interface i.e. serializable interface, here is a program that is serialized the object.

 public class Demo implements Serializable
  {
   string name;
   String address;
   transient int age;
  }

While making an object serialized two points to be remembered:

  • Class must implements java.io.Serializable interface.
  • All field must be serialized, if a field do not to be serialized then it should mark as transient.

 Code how to serialized an object  : To serialize the object ObjectOutputStream class is required. This program will not produce any output but it will create a file name demo.ser.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Demo implements Serializable 
{
	String name;
	String address;
	transient int age;
	Demo(String name,String address,int age)
	  {
		this.name=name;
		this.address=address;
		this.age=age;
	   }
    }
 public class Serialized
 {
	 public static void main(String args[])
	 {
		 Demo ob=new Demo("Rose","Rohini",23);
		 System.out.println(ob.name);
		 System.out.println(ob.address);
		 System.out.println(ob.age);
		 try
	      {
	         FileOutputStream fileOut =
	         new FileOutputStream("Demo.ser");
	         ObjectOutputStream out =
	                            new ObjectOutputStream(fileOut);
	         out.writeObject(ob);
	         out.close();
	          fileOut.close();
	      }catch(IOException i)
	      {
	          i.printStackTrace();
	      }
	   }
	 }

Code to deserialize the object:  While deserializing the object, ObjectInputStream class method is used and it will read the object state and prints the output:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Demo implements Serializable 
{
	String name;
	String address;
	transient int age;
	Demo(String name,String address,int age)
	  {
		this.name=name;
		this.address=address;
		this.age=age;
	   }
    }
 public class Serialized
 {
	 public static void main(String args[])
	 {
		 Demo ob=new Demo("Rose","Rohini",23);
		 try
		 {
		 FileInputStream fileIn = new FileInputStream("Demo.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
          ob = (Demo) in.readObject();
          in.close();
          fileIn.close();
           }catch(IOException i)
            {
        	   i.printStackTrace();
              return;
            }
	        catch(ClassNotFoundException c)
	        {
             System.out.println("Demo class not found");
              c.printStackTrace();
             return;
           }
        System.out.println("Deserialized Details...");
        System.out.println("Name: " + ob.name);
        System.out.println("Address: " + ob.address);
        System.out.println("age " + ob.age);
	  }
   }

When you compile and execute the program then it will produce the following output :

In the above output we can see that while serializing the object the value of age is 23 but when it is deserialized the age value is not displayed because it is transient, and transient value will not serialized. So it is displaying 0 of age value.