2021-06-08 14:23:48 +02:00
Instances of a ``++Serializable++`` class can be saved out to file and rehydrated at leisure. But that only works when all the values in the class instance are themselves ``++Serializable++`` (or ``++transient++``). Storing a non-serializable value in a ``++Serializable++`` class will prevent the serialization of that class.
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,java]
2021-06-08 14:23:48 +02:00
----
interface Fruit extends Serializable {...}
class Gooseberry implements Fruit { // Nonserializable because of Thread field
Thread thread;
}
class Bowl implements Serializable {
private static final long serialVersionUID = 1;
private Fruit fruit = new Gooseberry(); //Non-Compliant
}
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,java]
2021-06-08 14:23:48 +02:00
----
interface Fruit implements Serializable {...}
class Gooseberry implements Fruit {...} //Serializable
class Bowl implements Serializable {
private static final long serialVersionUID = 1;
private Fruit fruit = new Gooseberry(); //Compliant, Gooseberry is serializable
}
----
ifdef::env-github,rspecator-view[]
2021-06-08 15:52:13 +02:00
'''
2021-06-08 14:23:48 +02:00
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]