tags:

views:

118

answers:

4

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.

+1  A: 

What Is The Use Of A Marker Interface Even It Doesn't Have Methods...

http://www.blurtit.com/q250664.html

Discover the secrets of the Java Serialization API

http://java.sun.com/developer/technicalArticles/Programming/serialization/

ratty
+2  A: 

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.

Stephen C
Yes but the java system KNOWS that Thread is not serializable, why doesn't it manage this automatically ?
How would I serialize by accident since I have to apply a method write to the object I want to serialize ? I mean I have to do the jobs twice this doesn't adhere to the KISS principle ?
1) Actually, it doesn't. Or more precisely, it only knows this because Thread does not implement Serializable. 2) If you forget that object A may contain a reference to object B under some circumstances.
Stephen C
It doesn't just because SUN was to lazy to make it know itself :) Why doesn't SUN integrate this knowledge "KNOW THUSELF" is what makes a system intelligent.
No. This is not laziness, it is by design. The designers believed that it is safer for classes to be not serializable by default than to be serializable by default. For a number of reasons ... including security.
Stephen C
IMO, if there's any laziness ... it is you not wanting to tag your classes as serializable :-). Besides, I find it hard to imagine a Java application where *all* application classes should be serializable.
Stephen C
+1  A: 

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.

bjoern.bauer
A: 

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.

EJP
and answer like this is also pointless. The language may have been designed this way just because the team didn't have time to integrate in this version or for a fundamental reason.
It seems that other persons want to know so don't take your opinion for granted.