Re: Hi i am working on a project which has to do some serialization
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.