
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
56 lines
1.3 KiB
Plaintext
56 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Serializing a non-``++static++`` inner class will result in an attempt at serializing the outer class as well. If the outer class is not serializable, then serialization will fail, resulting in a runtime error.
|
|
|
|
|
|
Making the inner class ``++static++`` (i.e. "nested") avoids this problem, therefore inner classes should be ``++static++`` if possible. However, you should be aware that there are semantic differences between an inner class and a nested one:
|
|
|
|
* an inner class can only be instantiated within the context of an instance of the outer class.
|
|
* a nested (``++static++``) class can be instantiated independently of the outer class.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public class Pomegranate {
|
|
// ...
|
|
|
|
public class Seed implements Serializable { // Noncompliant; serialization will fail
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
public class Pomegranate {
|
|
// ...
|
|
|
|
public static class Seed implements Serializable {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://wiki.sei.cmu.edu/confluence/x/ZTdGBQ[CERT SER05-J.] - Do not serialize instances of inner classes
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Make this inner class static
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|