This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static method" Watch "static method" New topic
Author

static method

Krishna Tota
Ranch Hand

Joined: Mar 22, 2008
Posts: 40
Hi,
1)Can we synchronize a static method?if yes what is the effect on multithreading?
2)say,we have
class Abc{
static Myclass myobject=new Myclass();
}
now,if we use synchronized static method to access the myobject using one thread.
Can we use that myobject of that Myclass in another thread cuncarently:

Dont forget the method in Sunchronized.
please explain in brief.
Thanks.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Krishna Tota:
2)say,we have
class Abc{
static Myclass myobject=new Myclass();
}
now,if we use synchronized static method to access the myobject using one thread.
Can we use that myobject of that Myclass in another thread cuncarently:


myobject is an instance of Abc.so you can access this from different method concurrently. you are talking about myobject only right?
Shikhar Madhok
Ranch Hand

Joined: Dec 18, 2006
Posts: 95
Originally posted by Krishna Tota:
Hi,
1)Can we synchronize a static method?if yes what is the effect on multithreading?
2)say,we have
class Abc{
static Myclass myobject=new Myclass();
}
now,if we use synchronized static method to access the myobject using one thread.
Can we use that myobject of that Myclass in another thread cuncarently:

Dont forget the method in Sunchronized.
please explain in brief.
Thanks.


(My guess)
If the only way to access myobject is through a method which is synchronized, then you will be able to access it only from one thread at a time.
I think a static method can be synchronized as well.

HTH
Shikhar


He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever - Chinese proverb
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Can we synchronize a static method?if yes what is the effect on multithreading?


Yes we can synchronize on a static method, the effect is mutual exclusion of threads.Similar to synchronizing on non static methods.

originally posted by seetharaman venkatasamy
myobject is an instance of Abc.

No its not instance of Abc
its instance of Myclass
myobject=new Myclass();


And in both cases it cannot be accessed concurrently by another thread.

Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Amit Ghorpade:

And in both cases it cannot be accessed concurrently by another thread.

Hope this helps



Thanks Amit but myObject(static) is an instance .so it is not synchronized right?so why can not access that concurrently?

please ,can you explain me?
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

but myObject(static) is an instance .so it is not synchronized right?


Aren't instance accesses synchronized?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Got it Amit ... Thanks
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

You are welcome
 
Consider Paul's rocket mass heater.
 
subject: static method
 
Similar Threads
Another Question on Threads
static object refrences and non static object refrences
Instance attribute initialization
Run Time Object Identification
ArrayList class