Re: serialisation panic
Thomas Fritsch wrote:
Roedy Green schrieb:
I just woke up with some panicky thoughts about serialisation. Some
people dream about women; I dream about Java.
Poor man ;-)
1. If a class does not have a no-arg constructor, how is it possible
for serialisation to reconstitute the object?
It is not. See the API doc of Serializable:
<quote>
During deserialization, the fields of non-serializable classes will be
initialized using the public or protected no-arg constructor of the
class. A no-arg constructor must be accessible to the subclass that is
serializable.
</quote>
This says "of non-serializable classes". It doesn't speak to serializable
classes.
AFAIK there is no problem deserializing an instance of a class that lacks a
no-arg constructor as long as the class implements java.io.Serializable.
2. IIRC constructor initialisation does not happen but what about
STATIC fields? Do they get initialised? If so, how? Does it call
Static fields are initialized when the class is loaded, which will have
happened already by the time you're trying to deserialize any instances.
the <clinit> static-initialiser method directly?
I would assume so, since <clinit> is called at class-load-time.
But this has nothing to do with deserialization, of course.
3. how does initialisation set the private fields of an object?
Via the special in-built deserialization mechanism, with possible help from
the readObject() and readResolve() methods. The deserializer pulls the values
of the private fields from the input stream.
--
Lew