1

Possible Duplicate:
what does the keyword “transient” means in java?

I read about transient keyword in java. What I understood is?. It was used for fields. If we declare transient keyword that field will not be serialized. When does the serializable was happens in java for an field?. How it happens without I am extending Serializable interface?. Please correct me If I say anything wrong. This question may be possible duplicate.

  • Kannan
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
kannanrbk
  • 6,964
  • 13
  • 53
  • 94
  • So instead of searching SO you just added the disclaimer 'This question may be possible duplicate.'? – home Jan 18 '13 at 17:08
  • 1
    He was aware of terminology transient; but he wanted to double confirm that "if the class does not extend Serializable, then transient does NOT hold any significance". This fact is not mentioned explicitly anywhere.. +1 – Deepak Singhal Jan 18 '13 at 17:10
  • @Deepak I searched about transient in java and read the questions about transient in stackoverflow. Sorry , I am not clear after read . – kannanrbk Jan 18 '13 at 17:10
  • @bharathi That's what I said supporting your question and even upvoted it. – Deepak Singhal Jan 18 '13 at 17:13

3 Answers3

2

Serialization is needed when you want to transfer an object from Java heap to a flat file or on Network; basically whenever object needs to get out of JVM and needs to be stored in a way so that it can be re-created again.

So, now if there is any variable which you feel need NOT be populated when the object is created again after serialization then you declare it as transient. For example temperature on an object. We know that if we re-create the object any way this current temperature doesnt hold any significance so why to serialize it during process of serialization.

Deepak Singhal
  • 10,568
  • 11
  • 59
  • 98
2

You would use it for fields you want to exclude from serialization. Serialization only applies to classes where either the class or a class in its class hierarchy implements the interface. For instance, java.lang.Throwable implements java.io.Serializable, so all Errors and Exceptions inherit that marker interface.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
1

when you serialize an object you use an ObjectOutputStream to "write it down" and an ObjectInputStream to "read it back".

Usually the default behavior of the Java implementation is good enough for much of the "common use" of serialization. transient keyword tells the JVM you do not want it to save (or restore) the value of the variable. You should put extra-care when handling transient variables: after a restore of your serialized instance they will likely be null or with inconsistent values. It is good practice to add to your serializable class

 private void writeObject(java.io.ObjectOutputStream out) throws IOException{
    out.defaultWriteObject();
}

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{
    in.defaultReadObject();

    //here do something to re-initialize transient variables

}

this way you can insert a special handling for all you declared transient. (Remember that the constructor of your serializable class is called ONLY when you use it the first time, not when the object is read from ObjectInputStream)

In every other context which do not involves serialization transient does not means anything.

Viç
  • 356
  • 2
  • 5