Serialization in Java

In this article, we will learn about Serialization in Java with an example.

Java provides a mechanism called object serialization, in which an object can represent as a sequence of bytes that contains the object’s data as well as information about the object type and the types of data stored in it.

After a serialized object has been written to a file, it can be read from the file and deserialized, that is, the type of information and bytes representing the object and its data uses to recreate the object in memory.

Most impressive is that the whole process is JVM independent, which means that an object can be serialized on one platform and deserialized on a completely different platform.

The ObjectInputStream and ObjectOutputStream classes are high-level streams that contain the methods for serializing and deserializing an object.

The ObjectOutputStream class contains many write methods for writing different types of data, but one method stands out in particular

public final void writeObject(Object x) throws IOException

The above method serializes an object and sends it to the output stream. Likewise, the ObjectInputStream class includes the following method to deserialize an object

public final Object readObject() throws IOException, ClassNotFoundException

This method gets the next object from the stream and deserializes it. The return value is Object, so you need to convert it to the correct data type.

To show how serialization works in Java, I’ll be using the Employee class we talk about at the start of the book. Suppose we have the following Employee class that implements the Serializable interface

Example

public class EmployeeDetails implements java.io.Serializable {
   public String name;
   public String address;
   public transient int SSN;
   public int number;
   
   public void DataCheck() {
      System.out.println("Details are:  " + name + " " + address);
   }
}

Note that two conditions must be met for a class to be serialize successfully

The class must implement the java.io.Serializable interface.

All fields of the class must be serializable. If a field is not serializable, it should be mark as temporary.

If you want to know whether a standard Java class is serializable or not, see the documentation for the class. The test is simple: if the class implements java.io.Serializable, then it is serializable otherwise, it is not.

Serialization of an object

The ObjectOutputStream class is used to serialize an object. The following SerializeDemo program instantiates an Employee object and serializes it to a file.

When the program finishes, a file called Employee.ser is created. The program does not produce any output, but studies the code and tries to find out what the program is doing.

When serializing an object to a file, it is standard Java convention to give the file a .ser extension.

Example

import java.io.*;
public class SerializeExample{

   public static void main(String [] args) {
      EmployeeData e = new EmployeeData();
      e.name = "Handy Opinion";
      e.address = "Hany Opinion, ABC";
      e.SSN = 12345678;
      e.number = 10;
      
      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 under /tmp/employee.ser");
      } catch (IOException i) {
         i.printStackTrace();
      }
   }
}

That’s all, we will learn about Serialization in Java with an example.

Next Article

1. List Interface in Java with Examples
2. Deque interface in Java with Example

Please share this post:
Posts created 74

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: