wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Regarding serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding serialization" Watch "Regarding serialization" New topic
Author

Regarding serialization

surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
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

Joined: Oct 13, 2005
Posts: 36579
    
  16
Why should you want every class to be serializable?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1618
    
  10

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


When you do things right, people won't be sure you've done anything at all.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1618
    
  10

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

Joined: Sep 30, 2009
Posts: 30
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.

SCJP 1.6
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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

Joined: Sep 14, 2009
Posts: 1618
    
  10

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

Joined: Sep 30, 2009
Posts: 30
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

Joined: Sep 14, 2009
Posts: 1618
    
  10

Francisco, you're right. **sigh**

I gotta go get an umbrella.
Francisco Montes
Ranch Hand

Joined: Sep 30, 2009
Posts: 30
oh no! Now I´m feeling bad.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1618
    
  10

It's okay....

At least there's good reasons why all objects aren't Serializable.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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

Joined: Oct 13, 2005
Posts: 36579
    
  16
Do you really think that HFJ quote was a "hard-and-fast" rule?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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

Joined: Oct 13, 2005
Posts: 36579
    
  16
Don't disagree with Janeice. Disagree with me. After all, everybody else does.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Campbell:

LOL

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1618
    
  10

Ehhh..... disagreement is cause for discussion. Someone disagreeing with me is a blessing around here!
surya.raaj prakash
Ranch Hand

Joined: Oct 30, 2009
Posts: 76
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

Joined: Mar 09, 2009
Posts: 600
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding serialization
 
Similar Threads
Magic behind the serializable interface
why wouldn't all Java classes be serializable
regarding Serializable
Why does serialization require a serial version UID in the subclasses of the implementing class.