Java Serialization
Sujit Kumar
Zenolocity LLC
Copyright 2012 – 2050 @
Serializable Interface
• Marker Interface => No methods.
• Indicates that objects of the class can be
persisted (saved) as bytes on disk or
transmitted over the network as bytes.
• Objects can be reconstructed using the bytes
saved on disk or bytes received over the
network.
What does Serializable include?
• The entire object graph => current object, super
class attributes and objects contained within the
current object.
• Includes all the instance attributes (objects or
primitive types) from the object which are also
serializable.
• Includes all the instance attributes from the super
classes which are serializable.
• Excluded instance attributes marked as transient.
Requirements
• Class should implement the serializable
interface.
• Class should have a no argument constructor.
• serialVersionUID (static final long) is optional
but recommended. Represents the version
number of the class and checks compatibility
of serialized objects with the current version
of the class.
How to serialize & deserialize?
• ObjectOutputStream => writeObject()
• Saving to a file on disk => Wrap a
FileOutputStream with an
ObjectOutputStream.
• ObjectInputStream => readObject()
• Read from a file on disk => Wrap a
FileInputStream with an ObjectInputStream.

Java serialization

  • 1.
    Java Serialization Sujit Kumar ZenolocityLLC Copyright 2012 – 2050 @
  • 2.
    Serializable Interface • MarkerInterface => No methods. • Indicates that objects of the class can be persisted (saved) as bytes on disk or transmitted over the network as bytes. • Objects can be reconstructed using the bytes saved on disk or bytes received over the network.
  • 3.
    What does Serializableinclude? • The entire object graph => current object, super class attributes and objects contained within the current object. • Includes all the instance attributes (objects or primitive types) from the object which are also serializable. • Includes all the instance attributes from the super classes which are serializable. • Excluded instance attributes marked as transient.
  • 4.
    Requirements • Class shouldimplement the serializable interface. • Class should have a no argument constructor. • serialVersionUID (static final long) is optional but recommended. Represents the version number of the class and checks compatibility of serialized objects with the current version of the class.
  • 5.
    How to serialize& deserialize? • ObjectOutputStream => writeObject() • Saving to a file on disk => Wrap a FileOutputStream with an ObjectOutputStream. • ObjectInputStream => readObject() • Read from a file on disk => Wrap a FileInputStream with an ObjectInputStream.