Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
            "An inner class is a nested class that is not explicitly or implicitly declared static" \[[JLS 2005|AA. Bibliography#JLS 05]\]. Serialization of inner classes (including local and anonymous classes) is error- prone. According to the Serialization Specification \[[Sun 2006|AA. Bibliography#Sun 06]\],

...

In this noncompliant code example, the fields contained within the outer class are also serialized when the inner class is serialized.

Code Block
bgColor#FFcccc
public class OuterSer implements Serializable {
  private int rank;
  class InnerSer implements Serializable {
    protected String name;
    //...
  }
}

Compliant Solution

This The InnerSer class of this compliant solution omits implementation of does not implement the Serializable interface in the InnerSer class.

Code Block
bgColor#ccccff
public class OuterSer implements Serializable {
  private int rank;
  class InnerSer {
    protected String name;
    //...
  }
}

...

It is allowable to declare the inner class as static to prevent its serialization. It is also permissible for a static inner class to implement Serializable.

...