Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
40 lines (35 loc) · 1.39 KB

File metadata and controls

40 lines (35 loc) · 1.39 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Whenever we are serializing an object the set of all objects which are
* reachable from that object will be serialized automatically. This group of
* objects is nothing but object graph in serialization.
*
* In object graph every object should be Serializable otherwise we will get runtime exception saying"NotSerializableException".
*/
import java.io.*;
class I_am_rechable2 implements Serializable
{
int a=90;
int b=23;
}
class I_am_rechable1 implements Serializable
{
I_am_rechable2 obj2=new I_am_rechable2();
}
class I_will_serelize implements Serializable
{
I_am_rechable1 obj1=new I_am_rechable1();
}
public class ObjectGraph {
public static void main(String[] args)throws Exception {
I_will_serelize obj = new I_will_serelize();
FileOutputStream fos = new FileOutputStream("abc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
I_will_serelize iam_object = (I_will_serelize) ois.readObject();
//here we are serelizing I_will_serelizable object but all rechable object from this such as I_will_rechable1 and I_will_rechable2
//also get serelizable
System.out.println(iam_object.obj1.obj2.a + "_____________" + iam_object.obj1.obj2.b);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.