• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • 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.
 
Jan Cumps
Bartender
Posts: 2588
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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".
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • 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 ... ...
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4495
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
M. White
Greenhorn
Posts: 18
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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 .

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic