Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
      ...
}
like image 256
Joseph A. Avatar asked Dec 24 '13 18:12

Joseph A.


People also ask

Can ArrayList be serializable?

In Java, the ArrayList class implements a Serializable interface by default i.e., ArrayList is by default serialized. We can just use the ObjectOutputStream directly to serialize it.

Does List implement serializable?

List is not but implementation classes like ArrayLists are serializable. You can use them.

Can you serialize a list in Java?

In Java, ArrayList class is serializable by default. It essentially means that we do not need to implement Serializable interface explicitly in order to serialize ArrayList. We can directly use ObjectOutputStream to serialize ArrayList, and ObjectInputStream to deserialize an arraylist object.

Why is serialization implemented by list and ArrayList also?

ArrayList implements Serializable, so it can be serialized, that's exactly why the private backing array is transient, so it is not serialized along with other data in the class, since all is handled by ArrayList's writeObject and readObject methods.

Is ArrayList serializable in Java?

ArrayList is serializable by default. This means you need not to implement Serializable interface explicitly in order to serialize an ArrayList. In this tutorial we will learn how to serialize and de-serialize an ArrayList.

How to serialize an object in Java?

For serializing the object, we call the writeObject () method of ObjectOutputStream class, and for deserialization we call the readObject () method of ObjectInputStream class. We must have to implement the Serializable interface for serializing the object. Advantages of Java Serialization

Which class should implement Java IO Serializable interface?

Corresponding class should implement java.io.Serializable interface For pre-defined in-built Java classes, it should be implementing java.io.Serializable interface Exception: will be thrown if we try to serialize any class that doesn’t implement java.io.Serializable interface,

Is it possible to implement Serializable method in student class?

Here, you can see that Student class implements Serializable, but does not have any methods to implement from Serializable. Example: Below example code explains Serializing and Deserializing an object.


3 Answers

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.

like image 138
Suresh Atta Avatar answered Oct 19 '22 09:10

Suresh Atta


As others have stated, if a class is marked Serializable, then everything inside needs to be marked Serializable too.

But in this case also, you really need to make sure you limit mutability of internal state. In other words, when taking a mutable object (like ArrayList) in (a constructor or a setter) or returning it (in a getter), make copies to protect the internal state of MyClass. This is always a good practice but especially so with Serializable.

like image 24
Vidya Avatar answered Oct 19 '22 07:10

Vidya


Yes.

If you don't want that stuff to be serialized, mark it as transient.

like image 25
yamafontes Avatar answered Oct 19 '22 08:10

yamafontes