Hi i am working on a project which has to do some serialization
1 posts in topic
Flat View  Flat View

Posted By:   hima_bindu
Posted On:   Friday, February 25, 2005 06:25 PM

As there are no methods in java.io.serializable interface, how does java knows that it has to serialize the class which is implementing the serializable interface

Re: Hi i am working on a project which has to do some serialization

Posted By:   Christopher_Koenigsberg  
Posted On:   Saturday, February 26, 2005 09:40 AM

You have it backwards, it's tricky with this "marker interface". When Java serializes an object, it has to serialize all the member fields (unless "transient"). So, it is a kind of recursive descent, serializing member fields, also serializing their member fields, etc.

And so, when java needs to serialize an object (perhaps because it is a member field of a containing object being serialized), it will fail unless that object is an instance of a class that DOES implement the Serializable interface.

In other words, basically by NOT implementing Serializable in one class, you tell Java "object instances of this class can NOT be serialized").

And then if you have any other classes which have one of these non-serializable objects as a member field, they have to declare it "transient", or they in turn cannot be serialized either, even if they themselves DO implement Serializable.

So to me it's kind of backwards, basically the ABSENCE of "implements Serializable" is a "marker" saying "CAN NOT SERIALIZE THIS! BLOW UP, IF YOU GET HERE!" to the recursive-descent process of serialization, travelling down through member fields.
About | Sitemap | Contact