Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization and objects stored as instance variables

I'm a little confused about serialization.

I have a class, lets call it Container. Variables of the Container class contain ArrayLists of other classes - A,B,C,etc...

If I were to serialize and save Container, would I then, after reading it back in (and, for clarity, I understand how to do that part) have access to those same instances of classes A,B,etc or do they need to be separately serialized? Further, do A,B,etc need to implement Serializable is it that covered by Container doing so?

Thanks in advance

like image 971
MrB Avatar asked Nov 29 '25 15:11

MrB


1 Answers

This depends on how the Collection you are using is serialised.

If you are using an ArrayList then it will serialise all the items in the List.

In order to serialise the contents, the contents need to implement Serializable aswell otherwise they need to be marked transient.

So, to answer your two questions:

  • 1) If I were to serialize and save Container, would I then...have access to those same instances of classes A,B?
    Yes.
  • 2) do A,B,etc need to implement Serializable?
    Yes.
like image 82
Boris the Spider Avatar answered Dec 02 '25 06:12

Boris the Spider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!