Why can't just avoid this if I want all objects in my app to be serializable ?
Simply, because that's the way Java serialization works.
Consider that it does not make sense to serialize all objects.
Thread
instances and instances of most Stream
classes include critical state that simply cannot be serialized.
- Some classes depend on class statics, and they are not serialized.
- Some classes are non serializable because they critically depend on unserializable classes.
- Some classes you simply don't want or need to serialize.
So given that, how should the application programmer control what gets serialized? How does he stop all sorts of unnecessary stuff from being serialized by accident? Answer: by declaring the classes he wants to be serializable as implementing Serializable
.