What is the purpose of the Serializable interface?

22

Implementation example:

 public class MyClass implements Serializable{
     private static final long serialVersionUID = 1L;
 }
    
asked by anonymous 22.09.2015 / 15:50

3 answers

26

Serialization means saving the current state of the objects in binary format files to your computer, so that this state can be retrieved later by recreating the object in memory just as it was at the time of its serialization.

See the illustration:

  

Source: State of the art - Nuances about serialization of objects with inheritance in Java

In order to serialize and deserialize an object, it is mandatory for your class to implement the Serializable interface.

An example of a code that serializes an object:

import java.io.*;

public class SerializeDemo
{
   public static void main(String [] args)
   {
      Employee e = new Employee();
      e.name = "Reyan Ali";
      e.address = "Phokka Kuan, Ambehta Peer";
      e.SSN = 11122333;
      e.number = 101;

      try
      {
         FileOutputStream fileOut =
         new FileOutputStream("/tmp/employee.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in /tmp/employee.ser");
      }catch(IOException i)
      {
          i.printStackTrace();
      }
   }
}

Deserializing the same object:

import java.io.*;
public class DeserializeDemo
{
   public static void main(String [] args)
   {
      Employee e = null;
      try
      {
         FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Employee) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }
      System.out.println("Deserialized Employee...");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address);
      System.out.println("SSN: " + e.SSN);
      System.out.println("Number: " + e.number);
    }
}

Result:

  

Deserialized Employee ...
  Name: Reyan Ali
  Address: Phokka Kuan, Ambehta Peer
  SSN: 0
  Number: 101

Source: Tutorials Point

    
22.09.2015 / 16:04
9

It gives the class the ability to produce a format in which object data is used outside the code, it is usually persisted in some form of temporary or permanent storage, or is passed to another resource.

This format can be either text or binary in several standard or proprietary variants. It is very common to use JSON or XML.

Deserialization is the opposite process. It takes data in a known format and puts the data found in the serialized state inside the class members by creating or updating an object.

Implementing this interface is not very trivial, even though it seems to be. It is common to use a ready-made solution that uses reflection. It is common for serialization to expose private parts of the class, which is often not desirable.

    
22.09.2015 / 16:04
4

Adding the serializable interface will allow you to transform the object into a format that can be saved to a file. For example, to use an ObjectOutputStream and save an object to a disk file you will need to implement that interface.

    
22.09.2015 / 16:04