Serialization

  • a process in which current state of Object will be saved in stream of bytes
  • converting an object into a byte stream, to be saved in different mediums

Uses

  • Write to disk
  • Store in memory
  • Send byte stream to other platform over network
  • Save in databases as Binary Large Object (BLOB)

Serializable Interface

  • interface implemented when serialization is desired
    • Note that this is a Marker Interface
    • no methods must be defined to conform with it
  • Tells the compiler that the current class instance requires conversion to byte stream

Note

  • If a class implements Serializable, then all of the subclasses are Serializable automatically

  • If the superclass didn’t implement Serializable

    • The default constructor will be invoked from the superclass
    • All fields will initialize with the default values

serialVersionUID

  • version number associated to each serializable class by serialization runtime
  • used during deserialization process to verify that the sender and receiver of a serialized object have loaded class for that object which is compatible with respect to serialization.
  • a serialVersionUID field is not mandatory
  • if a serialVersionUID is defined
    • the field should be type long and must be static and final
  • if no serialVersionUID defined
    • the serialization runtime will calculate default value for the that class
    • this varies based on compiler implementation
    • advisable to define serialVersionUID

Non Converted Fields

  • Excludes specific fields from being serialized
  • use the transient modifier to exclude fields from being serialized
  • another way is to use static which also excludes from serialization