.class files provide blue-prints for each type of object, and Java has rich functionality for extracting the properties of an object, including all of its state.
So why we still need to implement serializable, can we just serialize automatically given the .class files?
.class files provide blue-prints for each type of object, and Java has rich functionality
Share
Serializableinterface in java is a Marker interface, which has no method or fields in it. It is simply implemented by class whose objects we may want to serialize.Moreover serialization is a costly affair because (Taken from Effective Java 2nd edition):
A major cost of implementing Serializable is that it decreases the flexibility
to change a class’s implementation once it has been released. When a
class implements Serializable, its byte-stream encoding (or serialized form)
becomes part of its exported API. Once you distribute a class widely, you are generally
required to support the serialized form forever, just as you are required to
support all other parts of the exported API.
A second cost of implementing Serializable is that it increases the likelihood
of bugs and security holes.
A third cost of implementing Serializable is that it increases the testing
burden associated with releasing a new version of a class. When a serializable
class is revised, it is important to check that it is possible to serialize an instance in
the new release and deserialize it in old releases, and vice versa.