Can a method be static and synchronized

Editorial / Best Answer

Answered by: Srimant Misra

  • Aug 12th, 2005


A static method can be synchronized. If you do so, the JVM will obtain a lock on the java.lang.Class instance associated with the object. It is similar to saying: 
synchronized(XYZ.class) 

}

Showing Answers 1 - 26 of 26 Answers

anupama

  • Jul 21st, 2005
 

why not!!! Static method can be synchronized.

nomaan khan

  • Aug 1st, 2005
 

Static methods can be synchronized. Synchronization has to deal with code which leads to deadlock or some uncertainity. The static methods can be accessed by diffrent threads. No problem in synchronization them 

  Was this answer useful?  Yes

Srimant Misra

  • Aug 12th, 2005
 

A static method can be synchronized. If you do so, the JVM will obtain a lock on the java.lang.Class instance associated with the object. It is similar to saying: 
synchronized(XYZ.class) 

}

Sisodiya Pramod

  • Aug 16th, 2005
 

Never, because synchronised is use for threads shareable data and thread is access data through object but static methods not called by object.

  Was this answer useful?  Yes

Yash Garg

  • Sep 11th, 2005
 

Yes a method can be specified as static as well as synchronized.

Example

class a

{

public

}

  Was this answer useful?  Yes

Yash Garg

  • Sep 11th, 2005
 

Yes a method can be specified as static as well as synchronized.

Example

class a

{

public static synchronized int function() {}

}

This is call class level synchronization.

  Was this answer useful?  Yes

Sunil Fotedar

  • Sep 12th, 2005
 

Each object has a lock associated with it. When any thread attempts to access a synchronized member, it must first acquire the associated objects lock. Once one thread owns the lock, no other thread may acquire the lock until the owning thread releases it which happens automatically when the synchronous operation completes. You can even make static members synchronized. Then the lock affects all objects of a given class, not just one particular instance.

Kshitish Pradhan

  • Oct 14th, 2005
 

Plz give the correct answer as we r referring.

  Was this answer useful?  Yes

varun paramala

  • Oct 14th, 2005
 

 a method be static  and synchronized . here the lock is acquired on the class it self instead of synchronized block(entire class will be under monitor)

  Was this answer useful?  Yes

SrinivasRao

  • Jan 17th, 2006
 

We Can give static and sychronized key words to a method

if u have any doubts

just run this program

u will know the truth

class  Class2
{
    public Class2()
 {
  
 }
 public static synchronized void aaa()
 {
  System.out.println("Example of staic synchronization of method");
 }
 public static void main(String[] args)
 {
 
  Class2.aaa();
  

 }
}

  Was this answer useful?  Yes

radhika

  • Jun 16th, 2006
 

yes it is working.

  Was this answer useful?  Yes

Niraj Lavankar

  • Mar 12th, 2007
 

No static method never be synchronized because static not realted with object,and deadlock condition occurs only when different  object of the same  class trying to acqire the same  resources at a single instance.

  Was this answer useful?  Yes

Java Mamu

  • Jun 20th, 2007
 

Yes, As the case with normal synchronized methods Static methods can be synchronised. The only difference here is that to call that method a Thread needs to get the Class Lock, because static methods are class members.

Thanks
Suhail

  Was this answer useful?  Yes

sadik

  • Jul 30th, 2007
 

hi please refer one more time static method's can be synchronized please go through the API u will find static synchronized method.

Thanks and Regards

  Was this answer useful?  Yes

Akii

  • May 21st, 2013
 

If you synchronized on static method ,its lock on class level not only that method.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions