Does Java Serialization work for cyclic references?

Brandon picture Brandon · Nov 24, 2009 · Viewed 10.4k times · Source

For example: Object A contains Object B that contains Object C that contains Object A.

Will Object A serialize properly?

Comment #9 here indicates that it does not work .

In contrast, XStream indicates that it does handle cyclic references.

Answer

Steven Schlansker picture Steven Schlansker · Nov 24, 2009

Yes, the default Java serialization works for cyclic references. When you serialize object C, the field will contain a backreference to the already-serialized object A instead of serializing it again.