3

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

2 Answers 2

3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I think I should hunt out some documentation on how Collections are serialised (not that that's your problem, of course).
0

As we know Serialization is the process of saving an object in a storage medium (such as a file, or a memory buffer) or to transmit it over a network connection in binary form. The serialized objects are JVM independent and can be re-serialized by any JVM.Read for good example here.

.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.