Java IO: Serializable
Jakob Jenkov |
The Java Serializable
interface (java.io.Serializable
is a marker interface your classes must implement if they are to
be serialized and deserialized. Java object serialization (writing) is done with the ObjectOutputStream
and deserialization (reading) is done with the ObjectInputStream.
That Serializable
is a marker interface means that it contains no methods. Therefore, a class
implementing Serializable
does not have to implement any specific methods. Implementing
Serializable
thus just tells the Java serialization classes that this class is intended for object
serialization.
Serializable Example
Here is an example of a class that implements the Java Serializable
interface:
import java.io.Serializable; public static class Person implements Serializable { public String name = null; public int age = 0; }
As you can see, the Person
class implements the Serializable
interface, but does
not actually implement any methods. As mentioned earlier, the Java Serializable
interface is just a
marker interface so there are no methods to implement.
To see a full example of serializing and deserializing an object that implements Serializable
,
see my ObjectInputStream tutorial.
serialVersionUID
In addition to implementing the Serializable
interface, a class intended for serialization should
also contain a private static final long
variable named serialVersionUID
.
Here is the Person
class from before, with a serialVersionUID
variable added:
import java.io.Serializable; public static class Person implements Serializable { private static final long serialVersionUID = 1234L; public String name = null; public int age = 0; }
The serialVersionUID
variable is used by Java's object serialization API to determine if a deserialized
object was serialized (written) with the same version of the class, as it is now attempting to deserialize it into.
Imagine that a Person
object is serialized to disk. Then a change is made to the Person
class. Then you try to deserialize the stored Person
object. Now the serialized Person
object may not correspond to the new version of the Person
class.
To detect such problems a class implementing Serializable
should contain a serialVersionUID
field. If you make big changes to the class, you should also change its serialVersionUID
value.
The Java SDK and many Java IDEs contains tools to generate the serialVersionUID
so you don't have to.
Object Serialization Today
In today's world (2015 and forward) many Java projects serialize Java objects using different mechanisms than the Java serialization mechanism. For instance, Java objects are serialized into JSON, BSON or other more optimized binary formats. This has the advantage of the objects also being readable by non-Java applications. For instance, JavaScript running in a web browser can natively serialize and deserialize objects to and from JSON.
These other object serialization mechanisms typically do not require your Java classes to implement
Serializable
, by the way. They are typically using Java Reflection
to inspect your class, so implementing the Serializable
interface would be superflous - it would
not add any useful information.
I have a separate tutorial about serializing and deserializing Java objects to and from JSON here: Java JSON Tutorial .
More Information About Serialization
Object serialization is kind of a topic in its own right. This Java IO tutorial is mostly focused on the streams and readers / writers. Therefore I will not get into deeper detail about object serialization at this point. In addition,a lot has already been written online about Java object serialization. Instead of repeating it, I'll give you a link to deeper explanations of the subject.
http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html |
Tweet | |
Jakob Jenkov |