File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes interview question of the day (08/04/2008) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "interview question of the day (08/04/2008)" Watch "interview question of the day (08/04/2008)" New topic

interview question of the day (08/04/2008)

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hi Guys,

Back from a long weekend with a new question for you.

Q. Do we have control on the process of de-serialization? Suppose I have a class that has 2 instance variables, out of two one is declared transient - so this variable don't take part in the process of serialization, now I serialize an instance of this class and then de-serialize the stream I got from serialization, so the transient variable will be null in the object we got from de-serialization (Right?), now can I control this process? Can I have something else than null in transient variable?
Rambo Prasad
Ranch Hand

Joined: Feb 23, 2006
Posts: 628
A suggestion...Instead of starting a separate thread for each question it would be better if you can continue posting the question in the same thread,else it would clutter the Jobs Discussion board and make it look like a technical forum...

Just my 2 cents...
[ April 08, 2008: Message edited by: Rambo Prasad ]

Helping hands are much better than the praying lips
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Rambo, good point. This forum is not meant for technical questions.

Off to the I/O forum it is.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

With the readResolve, readObject and writeObject methods you can have a quite extensive control over serialization and deserialization.

A little explanation on each:

readResolve can be used to return the same static instance for singletons. If present, this method is used to determine the result of deserialization.

Without the readResolve method, you can actually duplicate the Singleton object by serializing and deserializing it. Because the readResolve method returns INSTANCE, it will not duplicate it but return the one single instance.

Now you won't use this method much, unlike readObject and writeObject. These are mostly used to perform some extra steps when (de)serializing. They usually start with "in.defaultReadObject()" for readObject and "out.defaultWriteObject()" for writeObject. The (de)serialize the non-transient fields as usual. If you omit them nothing will be (de)serialized. What follows next is up to the programmer, but readObject usually (re)initializes transient values.


One note: these methods can be (and should be!) kept private, as the JVM will use reflection to call them. Don't worry about how at the moment, just that it works.
[ April 08, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: interview question of the day (08/04/2008)
It's not a secret anymore!