File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q on Dan's Mock Exam(Synchronized)

 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Which of the following modifiers can be applied to a field?
a. abstract
b. final
c. private
d. protected
e. public
f. static
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

g. synchronized is NOT one of the answers.
But don't you consider the synchronized blocks as a form of applying the synchronized to a field?

From the code above, isn't it that we are applying the synchronized modifier to the field b?
 
Alexan Kahkejian
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alton
synchronized can only apply for methods or code blocks but never can be applied for variables.
Regarding your example you are synchronizing the block of code on the variable b.So no more than one thread can access b at the same time inside this block only, but outside the block b still can be accessed from more than one thread.So you are synchronizing the block but not the var b.
Hope that helps
Alexan
[ June 16, 2003: Message edited by: Alexan Kahkejian ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alexan,
Thank you for your explanation.
However, the issue here is the phrase applied to a field in the question. As I see it, a synchronized block is applying the modifier synchronized to both field and code block.
[ June 16, 2003: Message edited by: Alton Hernandez ]
 
Alexan Kahkejian
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alton
If you refer to JLS 8.3 you will notice that Fields are member variables, so the rule that I've mentioned apply.
Alexan
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alton,
How do you feel about this new version of the question?

Which of the following modifiers can be applied to
the declaration of a field?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronized (Expression) Block
Maybe in this context, synchronized would not be called a modifier.
[ June 17, 2003: Message edited by: Marlene Miller ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marlene Miller:
synchronized (Expression) Block
Maybe in this context, synchronized would not be called a modifier.

I still consider it as a modifier because it is changing the behaviour of the block in relation to the object pointed to by the variable.
------
Originally posted by Dan Chisholm:
How do you feel about this new version of the question?
Which of the following modifiers can be applied to the declaration of a field?

This sounds about right to me.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alton Hernandez:
I still consider it as a modifier because it is changing the behaviour of the block in relation to the object pointed to by the variable.
You may consider it a modifier but Java doesn't. "Modifier" has a very specific meaning in Java as defined by the JLS.
 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alexan Kahkejian:
Hi Alton
If you refer to JLS 8.3 you will notice that Fields are member variables
Alexan

This means Fields reffer to methods & instance variables right?
Veena
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This means Fields reffer to methods & instance variables right?

A class has members. Members can by classified as:
1. Member data:
This include constant (final variable) or variables. Their values could be primitive literals, objects or arrays. Member data are also know as fields.
2. Member method:
3. Member class:
So the answer is No. Fields do not refer to methods.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic