I\'ve been learning how to use Serializable
.
I know if I create a class \'A\' with different variables who implements Serializable
and I ad
The serialization is actually implemented in java.io.ObjectOutputStream
(and java.io.ObjectInputStream) and some of its helper classes. In many cases, this built-in support is sufficient, and the developer simply needs to implement the marker interface Serializable
. This interface is called a "marker" because it doesn't declare any methods, and thus doesn't require any special API on implementation classes.
A programmer can add or replace default serialization mechanism with their own methods if needed. For example, if some additional initialization is required after deserializing an object, a method can be added with the following signature:
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, java.lang.ClassNotFoundException
For total control over serialization and deserialization, implement java.io.Externalizable
instead of Serializable
.
There are many other extension points in Java serialization, if needed. The serialization specification is an authoritative and complete source for learning about all of them.
If you implement a class which has to be serializable, you also have to provide a method which does the serialization in the same class.
You cannot rely on Object to be able to guess what your class needs to be successfully serialized and deserialized. Think of working variables of your class for example which do not need to be serialized, Object would not be able to differentiate those from the important fields.
I suppose the methods you are talking about are readObject()
and writeObject()
. You only need to implement those if you need to do custom serialization, for example when you have fields in your object that aren't serializable. If you only have serializable fields and primitives, you don't have to implement custom serialization methods.
Also, you can skip some fields on serialization by adding the transient
keyword to them.
For a class to be serializable, each object contained as a member of that class must also be serializable. Java will run down the tree of all objects referenced by yours and serialize them each in turn.
If you want to have greater control over how objects are serialized, you can implement the Externalizable interface:
The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes.
Look at the API doc of Serializable
, it explains the mechanism in detail.
Basically, you don't have to do anything unless you want more control over how your objects are serialized, in which case there are some "magic" methods you can implement and which will be called by the serialization mechanism.
If you want full control, you can use Externalizable instead.
I know if I create a class 'A' with different variables who implements Serializable and I add Serializable to my class, it's also Serializable.
Yes, this moment your class is Serializable.