Most of you must be aware with the Serialization involving single object but have you ever thought what if the object has reference to other objects too. Will it be serialized? Will reference objects will be serialized?
Answer to these queries is yes, you don’t have to explicitly serialize reference objects. Let’s find out how this can be achieved.
What is Object Graph?
An Object Graph is the set of objects which will be serialized automatically, if the object which contains reference to them is serialized.
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.
Lets make it more clear with the help of a simple example.
Value of i after Serialization is 10 Value of j after Serialization is 20
Note: In object graph every object should be serializable. If at-least one object is not serializable then we will get runtime execution saying NonSerializableExecution.
This article is contributed by Somya Garg. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Object Serialization with Inheritance in Java
- Serialization and Deserialization in Java with Example
- Customized Serialization and Deserialization In Java
- How to prevent Singleton Pattern from Reflection, Serialization and Cloning?
- Collator compare(Object, Object) method in Java with Example
- Java Program to Create an Object for Class and Assign Value in the Object Using Constructor
- Flexible nature of java.lang.Object
- Java Object Creation of Inherited Class
- Object class in Java
- Why Java is not a purely Object-Oriented Language?
- How to make object eligible for garbage collection in Java?
- Is an array a primitive type or an object in Java?
- Object level and Class level locks in Java
- Object toString() Method in Java
- String Literal Vs String Object in Java
- Merge arrays into a new object array in Java
- List remove(Object obj) method in Java with Examples
- ZoneOffset equal(Object) method in Java with Examples
- Stack indexOf(Object, int) method in Java with Example
- Stack add(Object) method in Java with Example