• Post Reply Bookmark Topic Watch Topic
  • New Topic

concept of serializable  RSS feed

 
gaurav chaudhary
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to get the concept of serializable. Can anybody plz explain why some objects have to be serializable? And the concept behind transient.
thanks in advance
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serializable classes are necessary for transmitting over the wire (usually the internet). Only instances of classes that implement Serializable are allowed to do that. It is also necessary if you want to persist an object to the local file system or database. A transient field is one that will not be included when the object is serialized. A good example of a field which should be transient would be a user's password or some other sensitive data.
[ January 04, 2004: Message edited by: Michael Morris ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possibly, this is more than the original person wanted to know, but...
The "transient" keyword affects default serialisation. That is, how the object gets serialised/deserialised if it does not have a readResolve() or writeReplace() method. In fact, it does ObjectInputStream.defaultReadObject() or ObjectOutputStream.defaultWriteObject(), I think. Fields that are transient are skipped by any of this.
But, perhaps you have a field that does need to be serialised, but not in the default format. Then, you might declare it "transient", but then add special handling for it in readResolve() and/or writeReplace().
One application for this, used by me, is to reduce the serialised size of an object. The default serialisation stores all sorts of information, beyond just the raw data. For instance, field names, classes etc. However, where performance is critical, you may add custom serialisation, where you just write the data, in your own, concise, format.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!