Does Java Serialization work for cyclic references?

前端 未结 4 1100
误落风尘
误落风尘 2021-01-01 18:16

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 d

4条回答
  •  情歌与酒
    2021-01-01 18:49

    You can actually view the referencing firsthand if you serialize your object to XML. The child objects are only serialized once. Any reference (anywhere in the serialized structure) to a child object that has already been serialized will simply point to that object in the file.

    Serializing cyclic references can get a little messy, though, so you might want to avoid them if you can.

提交回复
热议问题