• 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

Class synchronization

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I had problem with this question:::::::::



Which of the following statements are true ?

Options :
a . A static method may be invoked before even a single instance of the
class is constructed.
b . A static method cannot access non-static methods
c . Abstract modifier can appear before a class or a method but not
before a variable.
d . final modifier can appear before a class or a variable but not before
a method.
e . Synchronized modifier may appear before a method or a variable but
not before a class.



Given answer of this question is:::: a, b, c


But i think it should be :::: a, c, e

explaination::: for b:::::As static method is able to access non-static methods with the help of instance of class
e::: synchronized keyword can only used before methods and variables..


Plz explain where i m wrong


Thanks and regards,
Abhishek
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhishekxp-

You are correct in that a static method can access non-static methods via an instance of a class, but generally questions about what is accessible from a static method are not asking about that. They want to know what you can access directly - without any instance of a class. And in that case, the answer is that a static method can only access static methods and variables. Through an instance class they can access non-static methods and variables, but only through the instance. That is why b is true.

As for the synchronized keyword, it only applies to methods or blocks of code. It doesn't apply to variables. In the case of a method it preceeds the return type of the method.



In the case where it's a a block, the syntax looks like this:


Where this is the current object. this could also be replaced by a different object on which you want to synchronize.

While in the strictest sense of the question the synchronized keyword does appear before a variable in the case of a synchronized block that is synchronized on a variable, the question is really asking what things it is valid for the synchronized keyword to modify. And the answer to that is that the synchronized keyword can modify a method or a block of code.

Hope that helps,
Josh
[ February 05, 2006: Message edited by: Joshua Smith ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic