• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Static keyword Doubt

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all,

i have heard ,if we use static key word for any method or variable ,we have to call the method using the classname like,ClassName.variableName
But in the below program the static method sleep of thread called without class name.How it is possible?

Code:



Please help me.Thanks in advance.
 
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Static applied to the Class, it doesn't mean we can't access that property using the object of that class.

thanks & regards
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Muneeswaran Balasubramanian,

Since you extend Thread class all the public methods of thread class becomes the part
of the extended class. As you know to call local method of the class can be just called with their name
we call sleep methods. For more information please see
 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here you are extending the Thread class,So the child class directly inheriting a method from the parent.

If you try to implement the Runnable interface,then you will not be able to do the same.

Thanks
 
Greenhorn
Posts: 16
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static methods yield() and sleep() aren't inherited!

But as the Thread class is part of the inheritance tree of your class the compiler will invoke those methods from the Thread class.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in java there is a concept called "static import " if you was imported like static import then there is no need of class name to call any function . you can dirlty called any method which is static.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I do believe lambadi is right. Static methods are not inherited, so a subclass of Thread shouldn't be able to call it from the this reference. This should be an omitted static import.

I'm really really not a fan of static imports.
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code will clear all the doubts:


cheers
Mala
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic