[Biojava-l] serialization

Thomas Down td2@sanger.ac.uk
Mon, 31 Jul 2000 10:41:04 +0100


On Sun, Jul 30, 2000 at 07:09:19PM +0100, Matthew Pocock wrote:
> Dear all,
> 
> I am confused about serialization. Some BioJava objects need to have
> transient fields. When the object is de-serialized, these fields will be
> null. Does anybody know what methods I need to provide to set up the
> transient fields to usefull values during deserialization?

My understanding is that transient fields aren't necessarily
null-initialized, they get whatever value is set in the class
initializer.  But in any case...

Are there any specific instances you're thinking of.  The
case that comes to mind is Maps and things in objects which
implement caching.  The easy solution is.

public class ObjectWhichNeedsCaching implements Serializable {
  private transient Map myCache;

  protected Map getCache() {
    if (myCache == null)
      myCache = new HashMap();
    return myCache;
  }
}

Are there any cases in BioJava where this pattern isn't going
to work?  There /is/ also a method which can be called when an
Object is deserialized (can't remember offhand, it's in the
Serialization specs though), but I wouldn't recommend it unless
it's really needed.

ttfn,

Thomas (car being sorted out at the moment)
-- 
One of the advantages of being disorderly is that one is
constantly making exciting discoveries.
                                       -- A. A. Milne