GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 21 of 502    Print  
Can a method be static and synchronized

  
Total Answers and Comments: 15 Last Update: March 11, 2008   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Srimant Misra
 
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) 

}

Above answer was rated as good by the following members:
anjaliraj, sudhakarbaish, meSubhadip, geeksterrahul, talktoatish
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
July 21, 2005 01:53:57   #1  
anupama        

RE: Can a method be static and synchronized
why not!!! Static method can be synchronized.
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
August 01, 2005 06:15:33   #2  
nomaan khan        

RE: Can a method be static and synchronized
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

 
Is this answer useful? Yes | No
August 12, 2005 15:46:05   #3  
Srimant Misra        

RE: Can a method be static and synchronized
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)
{
}

 
Is this answer useful? Yes | NoAnswer is useful 4   Answer is not useful 0Overall Rating: +4    
August 16, 2005 04:51:03   #4  
Sisodiya Pramod        

RE: Can a method be static and synchronized
Never because synchronised is use for threads shareable data and thread is access data through object but static methods not called by object.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 4Overall Rating: -4    
September 11, 2005 21:20:05   #5  
Yash Garg        

RE: Can a method be static and synchronized

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

Example

class a

{

public

}


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
September 11, 2005 21:21:02   #6  
Yash Garg        

RE: Can a method be static and synchronized

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.


 
Is this answer useful? Yes | No
September 12, 2005 09:28:07   #7  
Sunil Fotedar        

RE: Can a method be static and synchronized
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.
 
Is this answer useful? Yes | No
October 14, 2005 05:15:53   #8  
Kshitish Pradhan        

RE: Can a method be static and synchronized
Plz give the correct answer as we r referring.
 
Is this answer useful? Yes | No
October 14, 2005 09:32:48   #9  
varun paramala        

RE: Can a method be static and synchronized

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)


 
Is this answer useful? Yes | No
January 17, 2006 09:43:45   #10  
SrinivasRao        

RE: Can a method be static and synchronized

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();

}
}


 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape