| 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 | ||
|---|---|---|
| ||
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 | ||
|---|---|---|
| ||
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.
...