Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TransientWriter  RSS feed

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
Ans:
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.
This code wont compile
Ans is 1 my choice
Please correct me if i am wrong
 
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First the public class should be K and not TransientWriter.
The code compiles (Linux redhat 7.1 kernel 2.4.3-12) but throws an exception at runtime

the line 33 is
TransientWriter tw2 = (TransientWriter) in.readObject();
So I don't know about the answer since the exception is thrown when you try to read...
I'm really not sure about what's going on here, anyone any insights ?
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah It compiles. My intention was that it fails at runtime
not at compile time
But here is the problem.
Author of this code says the answer is 3
Which i dont agree?
Any body?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact I did not see but yes the file tw.out is written and it contains the transient String...
Nothing is specified in the Java Tutorial or API about transient fields and Externalizable classes, so I don't know, I'll try to find something...

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code compiles and run in JDK 1.3.1 (Windows 95).
The correct answer is 3.
See http://www.javaranch.com/ubb/Forum35/HTML/000369.html
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nain, it cleared my doubts too.

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx guys
I wasnt very familiar with the Externizable in the first place
The reason i didnt agree with the choice 3 was
becoz of the runtime exception issue
This URL you sent helped

Thankyou all
ragu
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The runtime exception was due to the fact that an Externalizable object calls the default constructor before calling readExternal. So this constructor must be public. The constructor generated automatically by the compiler gets the same access that its class, that was the problem.
 
What are your superhero powers? Go ahead and try them on this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!