• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Transient and Volatile modifiers to instance variables only?

 
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 16
Eclipse IDE Safari Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Lorraine Batol
Ranch Hand
Posts: 59
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic