Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

transient variable ? is there any other way

 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tekchand Prasad
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20551
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20551
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49445
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably too difficult a question for us beginners.
Moving.
 
Gaurav .Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Raf Szczypiorski

Thanks for sharing the link.
 
vicky chauhan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic