• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Transient Object is Serialized !!!

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone tell me why the below program Serialize the String s even
when it i declared transiet.
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.java"));
out.writeObject(tw);
ObjectInputStream in = new ObjectInputStream(new FileInputStream("tw.java"));
TransientWriter tw2 = (TransientWriter) in.readObject();
System.out.println(tw2);
}
}
Thanks
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shah,
Think it's because you implemented Externalizable. Try implementing Serializable.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Shah Chunky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Externalizable extends Serializable interface.
[This message has been edited by Shah Chunky (edited May 28, 2001).]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shah Chunky:

Externalizable extends Serializable interface.
[This message has been edited by Shah Chunky (edited May 28, 2001).]


Implementing Serializable with your
example does not output anything, and a null pointer
exception is thrown when you try to print out what tried
to write out and then to read back in.
So I think that if you implement Externalizable, then
it's up to you (the programmer)to check which variables
are static or transient before writing them out in
your writeExternal method. If you implement Externalizable,
I think it's also up to you to manage versioning and
to deal with the state of superclasses too (which is
done "automatically" by implementing Serializable).
Is this correct, Jane?
Ranjan
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get a runtime error (InvalidClassException) with the above code unless I substitute Serializable for Externalizable.
After this substitution, I get a NullPointerException when I run the code. If I remove the transient keyword, the code runs without errors.
I think this confirms what Ranjan posted.
 
Shah Chunky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Scott...
There was no problem when i ran my original code.
The Output was displayed.
Thanks
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But that its the point.
A transient field can not be SERIALIZED when you write out an OBJECT. You did not serialize it, you externalized it. You called a method internal to the class that did a writeExternal on one of it's own fields. A class that OWNS the field can decide what to do with that field. It is just when that class does NOT give you a way to write out the field that there is a problem.
From the API for EXTERNALIZABLE:


The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supercede customized implementations of writeObject and readObject methods.


 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic