Forums Register Login
transient Variable


In the question below the answer is 3.But i was under the notion that transient variables cannot be serialized.then how come the answer is 3.please do explain.thanks
import java.io.*;
public class TransientWriter implements Externalizable

private transient String s = "Hope I can ever be persistant!";
public void writeExternal(ObjectOutput oOut) throws IOException

public void readExternal(ObjectInput oIn) throws IOException, ClassNotFoundException
public String toString()
return s;
class K
public static void main(String args[]) throws IOException, ClassNotFoundException
TransientWriter tw = new TransientWriter();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("tw.out"));
ObjectInputStream in = new ObjectInputStream(new FileInputStream("tw.out"));
TransientWriter tw2 = (TransientWriter) in.readObject();
Attempting to compile and run the above code

1)will cause a compiler error due to the attempt to write a transient object.
2)will cause a runtime exception when an attempt is made to write a transient object.
3)will not cause any runtime error and the transient object is writen to the file named "tw.out".
4)will not cause any runtime error and the transient object is not written to the file named "tw.out". The program prints a blank line on the screen.
Transient and static variables are not serialized, true. But see in the question the object of class TranseintWriter(tw) is being serialized and answer 3 just mentions this fact.
Hope this helps,
Asma Zafar,
Sun Certified Programmer for Java2 Platform
[This message has been edited by Asma Zafar (edited September 10, 2001).]
When a class implements Externalizable interface, it takes
over the complete control of how to serialize the object via implemeting writeExternal() and readExternal() methods.
These methods will be invoked when you serialize/deserialize
the object via an ObjectOutputStream and ObjectInputStream
(see JDK API for Externizable for detail)
Now, take a look at these two methods in the class
that the code tries to serialize - TransientWriter class.
You will notice that they invoke the writeObject() and readObject() on the member variable, the String s.
So, regardless it is declared as private transient, the
code force it to be writen out and read in.

I agree with Nain Hwu. The "transient" keyword is applicable for serialization java serialization not if it is externizable. Since if a class implements the Serializable interface, the class doesn't have control over the actual serialization process, "transient" provides you a bit of control in how the java serialization algorithm works.
Basically as Nain mentioned, it boils down to the amount of control you want on the serialization process.
1. Use java serialization (thru implements Serializable) if you are ok with the default behavior.
2. Use transient variables if default serialization is good enough but there are things that just don't make sense serializing.
3. Provide your own hooks for serializing an object by implementing the Externalizable interface.
4. If java serialization is just too slow then use your own algorithm and define interfaces similar to Externizable ( this can get to be very tricky). One scenario is if you want to serialize objects in XML, or say you have proprietary formats,etc. Kind of acts as poor man's persistence layer. Note that performance gains occur usually because you don't need as generic a serialization algorithm as provided by java.
Don't listen to Steve. Just read this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java

This thread has been viewed 1652 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 20, 2019 07:40:44.