Skip to main content

Serialization

What

Process of converting Objects into byte array, so that they can be:

  • Transferred over network and be deserialized by another JVM instance
  • Flushed to disc and re-read later. e.g. we want to restore session(JSessionId) after restart

How

import java.io.ObjectOutputStream;

class MySerializable implements Serializable {
... properties
}

psvm {
ObjectOutputStream oos = new ObjectOutputStream(file_output_stream);
oos.writeObject(new MySerializable()); // Exception if the object does not implements Serializable
}

Relevance?

Personally not seen in practice. Application state is usually stored in DB's. Something like spring-session

Marker interface

Serializable interface does not have any methods - a marker interface. Another example is Cloneable.