Serialization is the process of converting an object's state (including its references) to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time. Serialization is used when you want to persist the object. It is also used by Java RMI to pass objects between JVMs, either as arguments in a method invocation from a client to a server or as return values from a method invocation, or as exceptions thrown by remote methods. In general, serialization is used when we want the object to exist beyond the lifetime of the JVM.
java.io.Serializable is a marker interface (has no body). It is just used to "mark" Java classes as serializable.
The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during de-serialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named serialVersionUID that must be static, final, and of type long:
<<accessSpecifier>> static final long serialVersionUID = 1L;
How to make a class eligible for serialization
To persist an object the respective class must implement the java.io.Serializable interface.
public class SerialClass implements Serializable
private static final long serialVersionUID = 1L;
private Date currentTime;
currentTime = Calendar.getInstance().getTime();
public Date getCurrentTime()
How to write an object into a file
Now we need to write this object to a file system. We use java.io.ObjectOutputStream for this purpose.