Search code examples
javaserializable

Java Serializable: Must E be serializable in ArrayList<E>?


I have the following class containing the fields specified below. My question is, must Admin, Worker and all my other self-defined classes implement Serializable for MyClass to be Serializable?

public class MyClass implements java.io.Serializable {
  private static final long serialVersionUID = 1L;
  ArrayList<Admin> admins;
  ArrayList<Worker> workers;
  ArrayList<Manager> managers;
  ArrayList<Secretary> secretaries;
  ArrayList<Category> categories;
  HashMap<Issue, HashMap<Category,Manager>> ManagedIssues;
  private static MyClass instance;
      ...
}

Solution

  • My question is, must Admin, Worker and all my other self-defined classes implement Serializable for MyClass to be Serializable?

    Yes. They have to be.

    ArrayList is already implements Serializable interface by default. But you need to implement Serializable interface for the types you are using.

    Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable.