File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on Class synchronization ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on Class synchronization ?" Watch "doubt on Class synchronization ?" New topic

doubt on Class synchronization ?

veesam sridhar
Ranch Hand

Joined: Oct 18, 2005
Posts: 51
Hi Ranchers,

I have a question on synchronization.
what happens if I write the following code.
public synchronized class Example{

what is the effect of the above class on its objects?

1)Can different threads create or access objects of Example class at the same time?

2)If I write the synchronized keyword for class does it mean all the static methods of the class will become synchronized? or all general methods will become syncronized?

3)If it is class level lock ,If it is class level lock does it effect on object creation process(like one thread at a time can create an object or only one object can be created for that class .... I am a bit confused ).

Please explain how the synchronization works in this particualr scenorio.

Thanks in advance,
Sridhar Veesam.

Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
Can we use synchronized modifier for class?
I think we can use it for methods and code block?right guys?
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 288
Yes Ankith is right!synchronized is illegal modifier for class.
only -public ,abstract and final is permitted...
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

I think sridhar seemed to be aware of the syntax error given by the compiler but still he wanted to know the effects. Am i right?

As such, you cannot see the real effects happening because the compiler stops you at the very first step during compilation by giving an error saying that

java:1: modifier synchronized not allowed here


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
I agree. Here's the link:
subject: doubt on Class synchronization ?
It's not a secret anymore!