Why can't just avoid this if I want all 开发者_开发技巧objects in my app to be serializable ?
Update: I know that some class cannot be serialized like thread but the java system KNOWS also that Thread is not serializable, why doesn't it manage this automatically ?
I'd like to know if there are some fundamental reasons.
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 mostStream
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
.
If you implement all your serialization-related code yourself, you don't need it, but as long as you do it using standard library functions, there must be some way to communicate that your classes are designed and ready for serialization. Just because all the classes in your program are serializable doesn't mean they are in other's programs.
Because that's the way the language was designed? Questions like this are fundamentally pointless. It would have been possible, and indeed easier, to make all classes serializable, but it wasn't done that way. There are lots of reasons why not, and they are given in some FAQ or Gosling interview somewhere, that I read about 12 years ago. Security was certainly one of them. But at this stage it's a futile discussion really.
精彩评论