Object Graph in Java Serialization
Last Updated :
30 Apr, 2025
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);
}
}
OutputValue 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.
Similar Reads
Object Serialization with Inheritance in Java
Prerequisite: Serialization, Inheritance Serialization is a mechanism of converting the state of an object into a byte stream. The byte array can be the class, version, and internal state of the object. Deserialization is the reverse process where the byte stream is used to recreate the actual Java
6 min read
Object Resurrection in Java
In object-oriented programming languages with garbage collection, object resurrection is when an object comes back to life during the process of object destruction, as a side effect of a finalizer being executed. Object resurrection causes a number of problems, particularly that the possibility of o
3 min read
Serialization of Lambda Expression in Java
Here we will be discussing serialization in java and the problems related to the lambda function without the serialization alongside discussing some ways due to which we require serialization alongside proper implementation as in clean java programs with complete serialization and deserialization pr
5 min read
NotSerializableException in Java with Examples
Serialization in Java is a mechanism of writing the state of an object into a byte-stream. It is mainly used in Hibernate, RMI, JPA, EJB, and JMS technologies. The reverse operation of serialization is called deserialization where byte-stream is converted into an object. The serialization and deseri
4 min read
How to Calculate Size of Object in Java?
In Java, an object is an instance of a class that encapsulates data and behavior. Calculating the size of an object can be essential for memory management and optimization. This process involves understanding the memory layout of the object, including its fields and the overhead introduced by the JV
2 min read
Object Class in Java
Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Ob
7 min read
SerialVersionUID in Java
The serialization at runtime associates with each serializable class a version number called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. G
5 min read
Understanding Classes and Objects in Java
The term Object-Oriented explains the concept of organizing the software as a combination of different types of objects that incorporate both data and behavior. Hence, Object-oriented programming(OOPs) is a programming model, that simplifies software development and maintenance by providing some rul
10 min read
Object toString() Method in Java
Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth, it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object, and if it extends another class, then it is
3 min read
Java IO ObjectStreamField
The Java. io. ObjectStreamField class is a description of a Serializable field from a Serializable interface. This class is serializationâs descriptor for classes. An array of The ObjectStreamFields is utilized to maintain the Serializable fields of a class. It includes the name and serialVersionUID
3 min read