*
The moose likes Beginning Java and the fly likes why we can not have synchronized block outside a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why we can not have synchronized block outside a method" Watch "why we can not have synchronized block outside a method" New topic
Author

why we can not have synchronized block outside a method

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

why we can have static or non static block out side any method but we cant have synchronized block in a class what problem will come if we will use.
for ex. below code fails as of now but it will work if put the synchronized block inside the main method


thanks
Tanu Gulati
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Tanu,

What would it mean if you wrote



?


cmbhatt
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You could put that code in a (static or non-static) initializer block.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

hey thanks Rob.
 
Consider Paul's rocket mass heater.
 
subject: why we can not have synchronized block outside a method
 
Similar Threads
Synchronizing threads
Can Free Floating code be synchronized
Lock an object concept que
can an array b synchronized
Synchronized Block