Open In App

Object Graph in Java Serialization

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, Serialization is the process of converting an object into a format that can be easily stored or transmitted like a byte stream. When we think of Serialization, we probably think that a single object is being serialized, but what happens if the object contains references to other objects? Will those reference objects be serialized too? The answer is yes, and we do not need to serialize the reference objects manually. Let's find out how this can be achieved.

What is Object Graph?

An Object graph is a group of objects that are linked through references, where one object points to another. In other words, we can say that when we serialize any object and if it contains any other object reference, then JVM serializes the object and as well as its object references.

Key feature of the Object Graph:

  • When an object is serialized, all referenced objects in the graph are also serialized automatically.
  • Every object in the graph must implement the Serializable interface otherwise, a NotSerializableException will be thrown.
  • We can use a transient keyword to prevent the serialization of specific fields.


Real-world Example:

Let's suppose we are saving a user profile and it contains references to orders, address, etc. Serialization will automatically serialize these connected objects as part of the object graph.

Note: Every object in the object graph must be serializable. If one object is not serializable, Java will throw a NotSerializableException.

Example:

Java
// Java program to demonstrate how serializing 
// an object serializes other reference objects.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

// Class Serial1 contains reference to object 
// of class Serial2. 
class Serial1 implements Serializable {
     Serial2 s2 = new Serial2(); 
}

// Class Serial2 contains reference to object 
// of class Serial3. 
class Serial2 implements Serializable {
     Serial3 s3 = new Serial3(); 
}

// A reference of this class is present in Serial2
class Serial3 implements Serializable {
     int i = 10;
     int j = 20;
}

class Geeks {

     public static void main(String args[]) throws Exception {

     // Creating object of class Serial1
     Serial1 s1 = new Serial1();

     // Serializing object of class Serial1
     // Saving object in file
     FileOutputStream fos = new FileOutputStream("abc.ser");
     ObjectOutputStream oos = new ObjectOutputStream(fos);

     // Method for serializing object of class Serial1
     oos.writeObject(s1);

     // Close streams once serialization is done
     fos.close();
     oos.close();

     // De-Serializing object of class Serial1

     // Reading object from file
     FileInputStream fis = new FileInputStream("abc.ser");
     ObjectInputStream ois = new ObjectInputStream(fis);

     // Method for de-serializing object of class Serial1     
     Serial1 serobject = (Serial1) ois.readObject();

     // Close streams once de-serialization is done
     fis.close();
     ois.close();

     // Printing values of i and j after Serialization
     System.out.println("Value of i after Serialization" + 
                               " is " + serobject.s2.s3.i);
     System.out.println("Value of j after Serialization" + 
                                " is "+serobject.s2.s3.j);
  }
}

Output
Value of i after Serialization is 10
Value of j after Serialization is 20

Note: Every object in the object graph must be serializable. If one object is not serializable, Java will throw a NotSerializableException.

Important Points about Object Graph in Java

1. All Objects must implement Serializable: It is very important that every class must implement the Serializable interface. If any object does not then Java will throw a NotSerializableExeption during serialization.

2. Using transient Keyword: When we do not want a specific field to seralized, in that case we use transient keyword.

Example:

Java
class Geeks implements Serializable {
    transient int skipField;
    int normalField;
}

Note: Here, skipField are skiped during serialization.

3. Circular References (Cyclic Graphs): Java can handle circular references automatically.

4. serialVersionUID: It is always recommended to declare a serialVersionUID in every serializable class.

Example:

private static final long serialVersionUID = 1L;

Advantages of Object Graph

  • Automatically serializes all objects in a graph.
  • Make sure the entire object hiereachy is saved.
  • Automatically manages circular references without causing infinite loops.

Disadvantages of Object Graph

  • If any object in the graph is non-serializable then the entire serialization process will fail.
  • Serializing large object graph can consume more resources.

Next Article
Article Tags :
Practice Tags :

Similar Reads