"An inner class is a nested class that is not explicitly or implicitly declared static" [JLS 2005]. Serialization of inner classes (including local and anonymous classes) is error prone. According to the Serialization Specification [Sun 2006]:
serialVersionUID values. The names assigned to local and anonymous inner classes are also implementation dependent and may differ between compilers.serialPersistentFields mechanism to designate serializable fields.Externalizable. The Externalizable interface requires the implementing object to manually save and restore its state using the writeExternal() and readExternal() methods.Consequently, programs must not serialize inner classes.
Because none of these issues apply to static member classes, serialization of static member classes is permitted.
In this noncompliant code example, the fields contained within the outer class are serialized when the inner class is serialized.
| 
public class OuterSer implements Serializable {
  private int rank;
  class InnerSer implements Serializable {
    protected String name;
    //...
  }
}
 | 
The InnerSer class of this compliant solution deliberately fails to implement the Serializable interface.
| 
public class OuterSer implements Serializable {
  private int rank;
  class InnerSer {
    protected String name;
    //...
  }
}
 | 
If an inner and outer class must both be Serializable, the inner class can be declared static. This prevents a serialized inner class from also serializing its outer class.
| 
public class OuterSer implements Serializable {
  private int rank;
  static class InnerSer implements Serializable {
    protected String name;
    //...
  }
}
 | 
Serialization of inner classes can introduce platform dependencies and can cause serialization of instances of the outer class.
| Rule | Severity | Likelihood | Remediation Cost | Priority | Level | 
|---|---|---|---|---|---|
| SER05-J | medium | likely | medium | P12 | L1 | 
Detection of inner classes that implement serialization is straightforward.
| CWE-499, "Serializable Class Containing Sensitive Data" | 
| [API 2006] | |
| 
 | |
| Item 74: "Implement serialization judiciously" | |
| [JLS 2005] | |
| [Sun 2006] | "Serialization specification", Section 1.10 The Serializable Interface |