This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Regarding accessing static methods." Watch "Regarding accessing static methods." New topic
Author

Regarding accessing static methods.

Ravi Kumar
Greenhorn

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);
t1.start();
try
{
t1.sleep(1000);
}
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
Hi,

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: http://aspose.com/file-tools
 
subject: Regarding accessing static methods.