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?
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 ]
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 ]
Joined: Apr 30, 2003
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
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.