rubixibuc
rubixibuc

Reputation: 7397

Serialization and pointers Java

When you serialized an object does it follow pointers. Let say I have a tree data structure and the root and all other object in the tree implement serializable. My class looks like this

class Tree ... private Node root: ...

Will it be able to follow the root being the only explicitly declared instance variable? Will it save the whole tree even though the pointed to elements aren't members? Also when it is restored will it recreate the data structure entirely?

Upvotes: 2

Views: 1178

Answers (1)

aroth
aroth

Reputation: 54806

Yes, if your root object and all its fields (and all the fields in those objects, and so on) are Serializable, then serialization will save the entire structure (by following each pointer/reference that it finds). The only things it won't save are any fields declared as transient or static.

Note that serialization is Java is even smart enough to handle circular references.

Upvotes: 7

Related Questions