• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Question on sleep() method from dan's

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have a look at the below code:


I thought as sleep() is a static method,it should always be invoked as Thread.sleep(),also I considered the syntax for sleep() above code results in compiler error... But it isn't it...why? Please put some light into it.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aishwarya,

The sleep method is static method of Thread class. Thats very true.
But who says that static method can only be called using ClassName.staticMethod()

You can always call static method from the instance. So you code:
sleep(500) is something like this.sleep(500);

which is very well justified.

Hope this clears.

Regards,
Sandeep Jindal
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a static method, it's good coding practice to call sleep() from the parent class Thread, but this is not required.

EXAM TIP: Note that sleep always executes on the currently running Thread. So when sleep() is called using a specific instance of Thread, it can be misleading. For example, if you have two Threads, t1 and t2, and within t1 is the statement, t2.sleep(), then it might appear that t2 is being put to sleep. But actually, the current Thread (t1) is put to sleep -- it just happened to call the static sleep() method from another instance.
[ September 16, 2004: Message edited by: marc weber ]
 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,

Thats nice explaination. Thats clears my boubt that why my IDEs give warnings when i used call a static method from an instance variable, here is the catch.

Regrads
Sandeep Jindal
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic