• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 628
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankur,
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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rambo, good point. This forum is not meant for technical questions.

Off to the I/O forum it is.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Example:

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.

Example:

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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic