This is a space that holds a collection of my personal work and ideas
Java Basic Serialization
Posted on 09/02/2018
Serialization includes the part that serializes an object to a file and deserializes a file to an object that has the desired same state as it was in. It is typically done by using two high-level classes from the java.io package - ObjectOutputStream and ObjectInputStream. In this article, we demonstrate the basic operations of serializing and deserializing.
Key Points in Serialization
A class must implement Serializable interface before its object can be serialized.
If a class implements Serializable, all its instances variables that have a reference to another object must be transient or the object referenced to must also implement Serializable.
If an instance variable is marked transient, it will not be serialized.
private void writeObject(ObjectOutputStream oos) and private void readObject(ObjectInputStream ois) could be used to manually add additional logic to the serialization process. This could be very useful to serialize objects that don't implement Serializable.
oos.defaultWriteObject() and ois.defaultReadObject() could be used before custom serialization logic happens to auto-serialize serializable objects.
oos.writeXXX() and ois.readXXX() could be used to write and read additional state in custom serialization process. The read order must be the same as the write order.
If a superclass implements Serializable, its subclasses do automatically.
If a superclass doesn't implements Serializable, when a subclass object is deserialized, the superclass constructor will be invoked along with its super-constructor(s). If the superclass contains constructor(s) with argument(s), it must explicitly define a nonargument constructor.