This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Transient and Volatile modifiers to instance variables only? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Transient and Volatile modifiers to instance variables only?" Watch "Transient and Volatile modifiers to instance variables only?" New topic
Author

Transient and Volatile modifiers to instance variables only?

Yin Stadfield
Ranch Hand

Joined: Oct 13, 2012
Posts: 35

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

Joined: Apr 12, 2013
Posts: 16

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

Joined: Jul 09, 2001
Posts: 1168
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


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Yin Stadfield
Ranch Hand

Joined: Oct 13, 2012
Posts: 35

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Transient and Volatile modifiers to instance variables only?
 
Similar Threads
Is volatile is only for instance variable? (error on ExamLab test 3)
Volatile Keyworld
error in K&B book?
transient and volatile variables
OCPJP sample question wrong on Oracles website