Java Object Serialization.

Serializable Objects

Requirements for objects to be serialized.

  • A serializable object must be an instance of a class that implements (or
  • defines an interface with no methods.
    public interface Serializable {}
    (Implementing the interface simply marks your class for special treatment by the Virual Machine) Example:
    public class Person implements {
    // members & methods implementation
  • Many of the Java standard classes are already serialized, and this is the reason that we in the previous session could serialize both the java.lang.String object and the java.util.Date object.

Objects must have a no-argument constructor.

  • The serialization mechanism uses reflection to construct the Serializable objects.
  • If the superclass(es) of a Serializable class is not Serializable, the superclass(es) must have a no-argument constructor (default constructor).
  • The serialization mechanism uses the no-argument constructor to initialize the member variables of the non-serializable superclass(es) of the object.
  • If the superclass(es) does not have a no-argument constructor, you can use the interface to work around this constraint.
    public class Box extends Rectangle implements Externalizable {
      private int height;
    //  required no-argument constructor for Externalizable mechanism
      public Box() {
        this(0, 0, 0);  // calls the next constructor w/arguments
      public Box(int width, int length, int height) {
        super(width, length); // calls the superclass constructor w/arguments
        this.height = height;
    // .... other codes
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.