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 Java in General and the fly likes wait() from within 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 » Java in General
Bookmark "wait() from within static method" Watch "wait() from within static method" New topic

wait() from within static method

Mike Rad

Joined: Nov 07, 2008
Posts: 2

I know that you can't call wait() from a static method, but what if you need to block your thread in a static method until some condition is met?

For example, lets assume you have a static getObject() method that returns some object instance but you want it to block if the makeObject() method hasn't been called yet to make the object?

public final class singleton{
private static singleton inst = null;
private singleton(){}
public static synchronized singleton getSingleton(){
// i want to block until it is not null without using a loop
return inst;

public static synchronized singleton makeSingleton(){
inst = new singleton();
return inst;
Henry Wong

Joined: Sep 28, 2004
Posts: 18534

synchronized static methods use the Class object to synchronize on. So, if you want to wait on the same object, you can use...



Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mike Rad

Joined: Nov 07, 2008
Posts: 2
Ahhhh, yes, using the class object works just fine. Thanks!
I agree. Here's the link:
subject: wait() from within static method
Similar Threads
Singleton class and instance methods
Looking for Object Oriented Solution
Singleton Class
A few questions on wait(), sleep(int) and InterruptedException
Singleton Design Pattern