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;
...
}
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.
List is not but implementation classes like ArrayLists are serializable. You can use them.
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.
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.
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.
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
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,
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.
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.
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
.
Yes.
If you don't want that stuff to be serialized, mark it as transient
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With