We know that, if we anticipate objects are passed by value over a remote method calls, or should we choose to serialize/deserialize the objects or want to save the object as a bytes to some file (and many other usecases), we are required to have our class implement Serializable. But lately I was just curious, if any cost is associated with the implementation of this marker interface. What if, we do not have any such requirements that are needed for Serialization, but still want to make it a serializable. I understand that there is no need to do anything that is absolutely not needed, but what if decide to make it Serializable. Would this decision cause some sort of unexpected behaviour?
I avoid Serializable like the plague. It completely breaks all encapsulation. Instead, if I want to save my objects, I usually add a methods like this to my class:
Or in the case of immutable objects:
Serializable makes your class' private fields part of the specification, which is absolutely horrible.
In that case, it would probably be a good idea to implement Externalizable. That way you get the benefits of it being Serializable (e.g. works with ObjectOutputStreams), but the class controls the read/write.
subject: Is there any cost associated with the implementation of Serializable interface?