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]\]:

  • Because inner classes declared in non-static contexts contain implicit non-transient references to enclosing class instances, serializing such an inner class instance results in serialization of its associated outer class instance.

...

  • Because inner classes cannot declare static members other than compile-time constant fields, they cannot use the serialPersistentFields mechanism to designate serializable fields.unmigrated-wiki-markup
  • Finally, because inner classes associated with outer instances do not have zero-argument constructors (constructors of such inner classes implicitly accept the enclosing instance as a prepended parameter), they cannot implement {{Externalizable}}. \[ The {{Externalizable}} interface requires the implementing object to manually save and restore its state using the {{writeExternal()}} and {{readExternal()}} methods.\]

None of these issues, however, apply to static member classes.

...

Attempts to serialize inner classes can cause instances of the outer class to be serialized and also introduce platform dependencies.

Rule Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

SER06-J

medium

likely

low

P18

L1

...