File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Why volatile variable can't be serialized? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why volatile variable can Watch "Why volatile variable can New topic

Why volatile variable can't be serialized?


Joined: Dec 22, 2005
Posts: 2
Why volatile variable can't be serialized?

====regards<br />subbu
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336


Welcome to JavaRanch!

Can you please update your display name so it complies with our Naming Policy. You can do this here.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Steve Fahlbusch

Joined: Sep 18, 2000
Posts: 602


For you, why would one serialize a variable?


Again for you, why would one specify a veriable as volitile?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63862

Please reserve this forum for truly advanced questions. Moved to Java in General (intermediate).

[Asking smart questions] [About Bear] [Books by Bear]

Joined: Dec 22, 2005
Posts: 2
In multithreaded environment to maintain the unique value of a particular field, we are using volatile.

Any reason why it can't be serialised.

If a particular memeber(field) declared as transient it will not be included while doing the serialization.
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336


Thank you for taking the time to update your display name. Unfortunately a valid display name must have both a real-sounding first and last name. Please can you update it again. Thank you.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
As far as I know, and I have just skim-read the "volatile" section of the language spec and the introduction to the Serialisation spec, the "volatile" keyword has no effect on serialisation.

The "volatile" keyword, as you say, is about maintaining a consistent view of the field between threads. It is "transient" that controls serialisation. I could find no evidence of any link between the two keywords.

Did you actually try to serialise your volatile field? I would expect it to succeed.

Note that you can serialise even transient fields. It's only the *default* serialisation mechanism that does not serialise them. If you provide custom serialisation, which ain't too hard, you can put anything you like in the serialisation stream.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257


Its a way to maintain the resources held by runtime objects.

Assume, you are using one file stream to read data in an object which you want to serialize.
Will it work after deserialization on other machine?. may or may not.

So all network,file,i/o,DB resources variables mainly used as transient variables, so that they will be reset to the runtime environemnt in which the object is deserialized.

Note: ofcourse to relase the resources too ...

More inputs are welcome.
I agree. Here's the link:
subject: Why volatile variable can't be serialized?
jQuery in Action, 3rd edition