• 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

Is volatile is only for instance variable? (error on ExamLab test 3)

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
on examlab test 3, reviewing my answers, I've read that volatile is a keyword applicable only to instance variables.

In fact, this is false, because if you try to compile a program with a static volatile variable, the program perfectly compile, and also in the javadoc there are sun examples of static volatile variables:

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html

Can someone confirm this to me ?

Thanks.
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have moved this post to our Mock Exam Errata forum.
That forum is dedicated to "problems with mock exams everywhere".
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point. The question should have been about member variables,
as compared to stack or local variables. And it's not just primitives.
Remember that a reference variable can be volatile too.

Jim ... ...
 
Sheriff
Posts: 7136
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Emanuele,

Thanks for the feedback, and sorry, I didn't see this post before
Can you please tell me the question number where you found this problem - if so, I can make it correct with the next release of ExamLab. For me, I couldn't find a statement like that in practice exam 3.

Thanks,

Devaka
 
Greenhorn
Posts: 18
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
same goes with volatile modifier, it compiles on a static member. As stated is K&B Study Guide 6 Page 72:

The transient modifier applies only to instance variables.
The volatile modifier applies only to instance variables.



As stated also in the url you given by Emanuele:


FieldModifiers:
FieldModifier
FieldModifiers FieldModifier

FieldModifier: one of
public protected private
static final transient volatile



Im not sure if this should be on a separate thread .

reply
    Bookmark Topic Watch Topic
  • New Topic