File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Regarding accessing static methods. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Regarding accessing static methods." Watch "Regarding accessing static methods." New topic

Regarding accessing static methods.

Ravi Kumar

Joined: Feb 12, 2007
Posts: 6
class Demo implements Runnable
public static void main(String args[])
//Create the object of Demo class.
Demo obj1 = new Demo()
Thread t1 = new Thread(obj1);
catch(InterruptedException e)

This code works.
Question : Even if sleep() method is static,we are accessing it via object of the class.why??
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
It is legal to call a static method via a reference to an instance. However, it is bad practice to do so, as it looks like an instance method call.

Just move on. Nothing interesting to see!

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311

Adding to the excellent previous reply:
As a programming teacher, I find that java newcomers are very confused by 'sleep()' method.
It is important to note that the thread going to sleep would be 'main' thread, and not t1...

You can test it by putting some infinite printing loop in both 'Demo' and 'main' (in main, it has to be after the 'sleep' connamd).
I agree. Here's the link:
subject: Regarding accessing static methods.
It's not a secret anymore!