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 transient and volatile variables 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 and volatile variables" Watch "transient and volatile variables" New topic

transient and volatile variables

Satish Ray

Joined: Jul 13, 2006
Posts: 16
In Kathy sierra SCJP 1.5 book i have read that transient and volatile modifiers apply only for instance variables.

Might be they are irrelevant in the following sense as I think:
1)transient ->during serialization only the object state is flattened and the class variables(I mean static varibles) wont come into picture here.
2)volatile->is meaningful only for instance varibles and not static variables, as only the objects state is being protected from any simultaneous modification.

Correct me if I am wrong.

I also compiled and ran the below program using static-transient, static-volatile combination of variables. it is perfectly compiling and running fine.

class A{
static volatile int x;
static transient int y;

public static void main(String[] args){
A a = new A();

Can any body please clarify my doubt?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
There are a number of uses of modifiers that do not cause compiler errors or runtime errors which are nevertheless totally meaningless - not applicable. So, what is your doubt? Did you expect the compiler to object?

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
I don't see why volatile wouldn't be useful for class variables.

I don't have the book, but perhaps what was meant is that those modifiers don't apply to local variables?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Satish Ray

Joined: Jul 13, 2006
Posts: 16

My doubt is whether we can use transient and volatile variables respectively along with static modifier legally? or is it that there is no necessity for using transient and volatile modifiers along with static modifier?
Henry Wong

Joined: Sep 28, 2004
Posts: 20526

The volatile modifier is perfectly legal for both instance and static variables -- as what it does, is specify how it should behave.

The volatile modifier doesn't make sense for local variables because local variables are not accessable by multiple threads.

The transient modifier doesn't make sense for static variables, because class variables are not serialized.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: transient and volatile variables
It's not a secret anymore!