aspose file tools*
The moose likes Java in General and the fly likes transient variable ? is there any other way Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "transient variable ? is there any other way" Watch "transient variable ? is there any other way" New topic
Author

transient variable ? is there any other way

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

In java if we do not want to save the state of a variable we use transient variable but is there any other way when you do not want to save the variable and also dont want to use transient variable.....i got it as an interview question


thanks
Tanu Gulati
Tekchand Prasad
Ranch Hand

Joined: Oct 20, 2005
Posts: 32
transient and volatile both are used to define non -serializable members of any class.means if we want to convert an object into byte stream and write into file we serialize the class but if any variable or method which we dont want to serialize we can specify them by above.note---VOLATILE is used with variable andTRANSIENT is used with methods.

Volatile is assigned with variable when we synchronze that variable in multi threading environment.It also indicate to have a master copy of it that is keep an updated value of this variable.
Transient we use when we want to serialize and object and that object keeps any thread or socket or any other type of member present in that object then we have to specify them transient so that JVM does not try to serialize them otherwise error will be thrown.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Tekchand Prasad wrote:transient and volatile both are used to define non -serializable members of any class.means if we want to convert an object into byte stream and write into file we serialize the class but if any variable or method which we dont want to serialize we can specify them by above.note---VOLATILE is used with variable andTRANSIENT is used with methods.

Where did you get this misinformation from?
Transient is a modifier that only applies to fields, and indicates that default serialization should ignore the field. It is used for serialization.
Volatile is a modifier that also only applies to fields, and indicates a field's value should never be cached but always the latest version should be retrieved. It is used for synchronization, not serialization.

Volatile is assigned with variable when we synchronze that variable in multi threading environment.It also indicate to have a master copy of it that is keep an updated value of this variable.
Transient we use when we want to serialize and object and that object keeps any thread or socket or any other type of member present in that object then we have to specify them transient so that JVM does not try to serialize them otherwise error will be thrown.

And then all of a sudden you get it right. How can you disagree with yourself so much?


Tanu,

If this weren't an interview question I wouldn't answer your question since that is exactly what transient is for; any other mechanism is just more work to achieve the very same thing.

You can completely control serialization yourself using two methods:
- private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
- private void writeObject(ObjectOutputStream stream) throws IOException

Usually, these methods are implemented as follows:

By omitting the first statement in each you can control (de)serialization completely:

As you see however, you will do all the hard work yourself. I haven't even included any fields from the parent class, another thing you get for free from default serialization. And finally, this custom deserialization will not let you restore final fields.

So if you ever get the choice - go for default serialization and transient fields.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
A much simpler approach than writeObject / readObject is this:
http://java.sun.com/javase/6/docs/platform/serialization/spec/serial-arch.html#6250
With this, you can define a field transient, and it could be serialized anyways, or you could not define a field transient, and have it ignored during serialization.

Hope this helps.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Raf Szczypiorski wrote:A much simpler approach than writeObject / readObject is this:
http://java.sun.com/javase/6/docs/platform/serialization/spec/serial-arch.html#6250
With this, you can define a field transient, and it could be serialized anyways, or you could not define a field transient, and have it ignored during serialization.

Hope this helps.

Heh. I didn't know that. Guess you've taught me something today
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39876
    
  28
Probably too difficult a question for us beginners.
Moving.
Gaurav .Singh
Greenhorn

Joined: Apr 06, 2010
Posts: 8
What if the transient attribute which i have maintained in my serialised class is String.

I override these methods -
private void writeObject(ObjectOutputStream os) throws IOException;
private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException;

using writeChars i am able to write string in to file
but how do i read it using ObjectInputStream --- as readObject() is only used for non transient values
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
@Raf Szczypiorski

Thanks for sharing the link.
vicky chauhan
Ranch Hand

Joined: Nov 27, 2008
Posts: 41
Went through the link on sun's site..

But can anyone explain the content in a simpler manner!!

I was unable to catch the content!!
 
 
subject: transient variable ? is there any other way