views:

53

answers:

2

When we are deserializing an object, its very difficult to understand that, how it is retriving the object in some certain state? Does it contain any Meta data of the object?

+4  A: 

When an object is serialized, the object's class is written to the stream along with the contents of the object's non-transient fields. The deserializer will attempt to load that class (and there are several mechanisms for it to do that), then populate the non-transient fields.

The protocol spec is here: http://java.sun.com/javase/6/docs/platform/serialization/spec/protocol.html

If by "metadata" you're referring to annotations on the class, then no, they are not serialized with the object itself, but are available on the class. If you mean something else, please describe what you mean.

kdgregory
Reckon that, every serialized object has its own meta data and by refering to that data from the stream deserialization happens.. "Metadata" i've mentioned here is the information about the attributes of the object.
i2ijeya
Unless you can describe "metadata" in terms of specific Java language features, the answer is "maybe"
kdgregory
A: 

At a high level, the serialization stream contains the data inside the object and the name of the classes involved, as well as a version number to ensure the class didn't change. It uses that information to make a new instance of an object and fills it with the same data as the old instance. It does this avoiding all of the usual constraints on object creation (the need to call constructors, for example).

One confusing point people have is that they can think the class definition itself is serialized. It is not, just the data it contains with enough information to know which objects to recreate when deserilalized. When the object is deserialized, it has to match the existing class on the class path, the serialization binary data does not contain the class.

Yishai