Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding serialization

 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Why serialization isn't default for all classes?and also why doesn't class Object implement Serializable,and all sub classes will be automatically Serializable.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should you want every class to be serializable?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surya.raaj prakash wrote:Hi Friends,

Why serialization isn't default for all classes?and also why doesn't class Object implement Serializable,and all sub classes will be automatically Serializable.


I HAD THE SAME QUESTION!
I will quote from HF Java....

Q: "If serializable is so important, why isn't it the default for all classes? Why doesn't class Object implement Serializable, and then all subclasses will be automatically Serializable?"

A: "Even though most classes will, and should, implement serializable, you always have a choice. And you must make a conscious decision on a class by class basis, for each class you design, to 'enable' serialization by implementing serializable.
First of all, if serializarion were the default, how would you turn it off? Interfaces indicate a functionality, not a lack of functionality, so the model of polymorphism wouldn't work correctly if you had to say 'Implements NonSerializable' to tell the world that you cannot be saved."

Hope that helps....
Janeice
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why should you want every class to be serializable?


I think everything SHOULD be serializable. Ahh.... to pack everything up for next time? PERFECT!
 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some classes are simply not useful to be serialized. Like classes that represent data that is too related with a specific runtime context: Threads, Streams, etc. Even if you saved them and then restored them again in the same VM their data (and internal memory references) would be useless since the whole runtime context changes on every run.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice:

I'm not sure that I buy that. Making a class serializable should be a conscious decision based on design criteria, rather than something done by rote.

John.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, what don't you agree with? My wanting to pack everything up for next time, or the HFJ quote?

I think there could hardly be anything more useful than to be able to pack up all your objects and variables... only to pull them out at a later date. All that time wasted on initializing and setting values, et cetera.... SAVED by packing things up in a nice little file.

Other than the runtime contexts as Francisco so kindly rained on my parade with, to be able to just come back to objects later (or even in another application!) makes my day sunny.
 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don´t get me wrong but ruining people´s sunny days is something I´d hate doing. :-)

I must admit that, while it is a nice and very tempting idea to keep objects packed up in one, or several, files I think i should play Devil´s advocate one more time.

Imagine that you have all your objects already serialized. Now, you feel the need to change your objects a little bit: adding an extra int instance variable here and there, adding static variables or removing some. Change happens. .

When you get this scenario, everything you had on file cannot be deserialized back into objects again because their definition has changed! You would, most probably, have to recreate your whole set of files with serialized objects everytime you make even the slightest change to their definition (which defeats the whole point of serializing them).

 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Francisco, you're right. **sigh**

I gotta go get an umbrella.
 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh no! Now I´m feeling bad.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's okay....

At least there's good reasons why all objects aren't Serializable.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janeice:

I'm disagreeing with the HFJ quote. What I'm disagreeing with is the idea that hard-and-fast rules should replace thoughtful decisions based on design requirements.

John.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really think that HFJ quote was a "hard-and-fast" rule?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell:

I guess my head's not in it today . I confused Janeice's argument with the HFJ, so obviously I was disagreeing with Janeice, not HFJ . My bad.

John.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't disagree with Janeice. Disagree with me. After all, everybody else does.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell:

LOL

John.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ehhh..... disagreement is cause for discussion. Someone disagreeing with me is a blessing around here!
 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends ,
Thanks For Your Replies.

I have one more Question,Why can't you save the network connections,threads and file objects?what will happen when you save them?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surya:

Think about the difference between a String (which implements Serializable) object and a Thread object, and about what kind of objects they are.

John.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic