Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Overriding static methods

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

Why cant static methods be overridden?

Thanks.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't post the same question in multiple forums, its confusing and makes the conversation difficult to follow. I think if you do a search in this forum, you'll find some answers people may have given in the past. I'm going to direct answers to this forum because I believe its the best match. Thanks
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Definitley check out Corey's blog entry: Static Methods CAN NOT be Overridden.
 
vanishree malagi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for the inconvenience caused.

the link provided was helpful, but it didnt clarify the reason behind the constraint.

thank you.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods belong to a class. If a instance inherits static method only can share it with others so a instance can`t override a static method.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are methods which are created and owned by a class itself, rather than an instance of a class.

Dynamic binding occurs with an instance of a class- it dynamically decides what type it is, then jumps on the virtual method table to the correct method definition.

Since static methods would be resolved at compile time, it does not use the virtual method lookup.

Now your question seems to be asking: why are static methods resolved at compile time?

Everything that can be resolved at compile time is done so- final variables are essentially inlined. But still, it seems to be a choice of the java language to resolve static methods at compile time, it does not seem ultimately required to have a functioning language.

One could imagine static methods being resolved at run time, when it made sense to do so (when you had a reference to a base class which was dynamically tied to one of the children classes) However, obviously this would not work if you were invoking the method without an instance of the class. If you do have an instance of the class, it seems like it would be possible, but would be a different language (Java++).

So I think the answer is: it was an arbitrary decision by the design comitteeto speed up the virtual machine (fewer dynamic bindings = faster) I agree with their decision, but could see the decision going the other way if history were to be split.
 
vanishree malagi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the explanation. i think now i can make some sense of it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic