• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

questions

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a questions regarding the SCJP exam:

1.
Can a static method be synchronized? If so, what is being locked
since there is no this reference?

2.
I thought one object has only one lock. Is that
not true?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.) Yes static methods can be syncronised.
they are synchronized on the class
eg-syncronized(<class name>

2.)Yes each object has only one lock
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anne Sullivan:
I have a questions regarding the SCJP exam:

1.
Can a static method be synchronized? If so, what is being locked
since there is no this reference?



From Kathy Sierra's and Bert Bate's SCJP book,


Static
methods can be synchronized. There is only one copy of the static data you�re trying
to protect, so you only need one lock per class to synchronize static methods�a lock
for the whole class. There is such a lock; every class loaded in Java has a corresponding
instance of java.lang.Class representing that class. It�s that java.lang.Class instance
whose lock is used to protect the static methods of the class (if they�re synchronized).



Hope this clears!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic