Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transient and Volatile modifiers to instance variables only?

 
Yin Stadfield
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to SCJP Sun Certified Programmer for Java 6 Exam 310-065 by Bert Bates and Kathy Sierra, page 72:
❑ The transient modifier applies only to instance variables.
❑ The volatile modifier applies only to instance variables.


But doing the following doesn't angry the compiler:

public static transient volatile int var;

So, it means even static variables (non-instance) can have transient and volatile modifiers or am I missing something?

Thanks!
 
Alex Benedict
Greenhorn
Posts: 16
Chrome Eclipse IDE Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure but I think that in such cases compiler will accept the code but static behavior will override the transient and volatile behavior. pl. correct if I am wrong.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can volatile variable be defined as static in java? says -


Declaring a variable as volatile (be it static or not) states that the variable will be accessed frequently by multiple threads. In Java, this boils down to instructing threads that they can not cache the variable's value, but will have to write back immediately after mutating so that other threads see the change. (Threads in Java are free to cache variables by default).


Regards,
Dan
 
Yin Stadfield
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Drillich wrote:Can volatile variable be defined as static in java? says -


Declaring a variable as volatile (be it static or not) states that the variable will be accessed frequently by multiple threads. In Java, this boils down to instructing threads that they can not cache the variable's value, but will have to write back immediately after mutating so that other threads see the change. (Threads in Java are free to cache variables by default).


Regards,
Dan


So I guess, it safe to assume that it doesn't apply only to instance variables, same goes for static as well. Thanks Dan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic