File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes transient Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "transient Variable" Watch "transient Variable" New topic
Author

transient Variable

Archana Nair
Greenhorn

Joined: Jul 24, 2001
Posts: 13

Hi

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
Archana
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
{
oOut.writeObject(s);
}

public void readExternal(ObjectInput oIn) throws IOException, ClassNotFoundException
{
s=(String)oIn.readObject();
}
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"));
out.writeObject(tw);
ObjectInputStream in = new ObjectInputStream(new FileInputStream("tw.out"));
TransientWriter tw2 = (TransientWriter) in.readObject();
System.out.println(tw2);
}
}
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.
Asma Zafar
Ranch Hand

Joined: May 11, 2001
Posts: 49
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).]


Asma Zafar,<BR>Sun Certified Programmer for Java2 Platform
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
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.

Prakash Yamuna
Greenhorn

Joined: Sep 30, 2001
Posts: 5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: transient Variable
 
Similar Threads
transient
Re: Few Questions from Abilash's mock exam
transient I.0
ob. serialization
TransientWriter