Serialization Java: which classes need to "implement Serializable"?

java, serialization

Solution

Yes, you are correct: Anything being serialized, including all classes referred to by fields (instance variables), must implement `Serializable`, or the field must be `transient`.

In your case, the user defined class must implement `Serializable`, and have fields whose type is `Serializable`. This applies recursively, so fields of the class of the fields must also be `Serializable`, etc.

The `transient` keyword tells the serialization mechanism to ignore the field for the purposes of serialization.

Problem

so I know I could probably figure it out eventually, but I couldn't really find a topic that answers this seemingly simple question. I am serializing a vector of a set of objects, with each such object pertaining to one user-defined class. I know vectors are serializable. Do I also have to make the user-defined class implement serializable? And, each of these user-defined classes contains other user-defined classes. Do those have to be labeled as implementing Serializable as well? (and so on, or do just the top-level class need to implement Serializable)? (Forget noting that arrayLists should be used instead of vectors, I've heard that before, I'm practicing because I hear vectors are good for multithreading. I found topics that discuss similar stuff, but not precisely this) Thanks

Original source