0

Let me put it in this way:

What does JVM understands when it see a class implementing Serializable

What could be the code written to handle this? (If class is Serializable)

Sunny Gupta
  • 6,929
  • 15
  • 52
  • 80

3 Answers3

2

Many classes cannot be serialized.
How can you serialize a UI element or a network socket or a thread?

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I didn't have much idea about Swing or AWT classes never worked on them, Could you please put some other example or elaborate this more – Sunny Gupta Dec 29 '11 at 16:58
1

You don't need to serialize (or) save state of every object. It doesn't make sense. Another reason could be instead of saying, by default, authors might thought let programmer decide. Here is interesting discussion on this from java team.

The decision to require that classes implement the java.io.Serializable interface was not made lightly. The design called for a balance between the needs of developers and the needs of the system to be able to provide a predictable and safe mechanism. The most difficult design constraint to satisify was the safety and security of Java classes.

If classes were to be marked as being serializable, the design team worried that a developer, either out of forgetfulness, laziness, or ignorance might not declare a class as being Serializable and then make that class useless for RMI or for purposes of persistence. We worried that the requirement would place on a developer the burden of knowing how a class was to be used by others in the future, an essentially unknown condition. Indeed, our preliminary design, as reflected in the alpha API, concluded that the default case for a class ought to be that the objects in the class be serializable. We later changed our design only after security and correctness considerations convinced us that the default had to be that an object not be serialized.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • Here is link for above content:http://java.sun.com/javase/technologies/core/basic/serializationFAQ.jsp. – kosa Dec 29 '11 at 17:11
0

There are a lot of examples of class which cannot be Serialized, not just those in the GUI.

From java.lang

ClassLoader, Compiler, Math, Package, Process, Runtime, Shutdown, StrictMath, System, Thread, ThreadGroup, ThreadLocal, Void

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130