| Author |
Is volatile is only for instance variable? (error on ExamLab test 3)
|
Emanuele Ghe
Ranch Hand
Joined: Feb 04, 2009
Posts: 111
|
|
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.
|
SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
|
 |
Jan Cumps
Bartender
Joined: Dec 20, 2006
Posts: 2350
|
|
I have moved this post to our Mock Exam Errata forum.
That forum is dedicated to "problems with mock exams everywhere".
|
OCUP UML fundamental
ITIL foundation
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
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 ... ...
|
BEE MBA PMP SCJP-6
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2729
|
|
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
|
Author of ExamLab (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
|
 |
M. White
Greenhorn
Joined: Dec 27, 2009
Posts: 18
|
|
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 .
|
 |
 |
|
|
subject: Is volatile is only for instance variable? (error on ExamLab test 3)
|
|
|