| 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]\],: |
- Serializing an inner class declared in a non-static context that contains implicit non-transient references to enclosing class instances results in serialization of its associated outer class instance.
...
The InnerSer class of this compliant solution does not deliberately fails to implement the Serializable interface.
| Code Block |
|---|
|
public class OuterSer implements Serializable {
private int rank;
class InnerSer {
protected String name;
//...
}
}
|
Compliant Solution
It is allowable to declare the The inner class may be declared static to prevent its serialization. It is also permissible for a A static inner class to may also implement Serializable.
| Code Block |
|---|
|
public class OuterSer implements Serializable {
private int rank;
static class InnerSer implements Serializable {
protected String name;
//...
}
}
|
...