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

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
 
Did you miss me? Did you miss 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