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.