• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

*why* can't static methods be overridden?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I understand that static methods can't be overridden, but why? What disaster would happen if you were allowed to override a static method?
Thanks! Karen.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are class levele methods and hence can't be over ridden . OOPS concepts like inheritance work on objects not on classes .
static is provided so that you don't need to create objects of the class . If objects aren't created then how will you have inheritance
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are accessed using class name and not from an instance of a class.

It therefore follows that the class type for the static method is the one that is set at complie time. As for the non-static (and non-final) methods, it is the runtime type of the class that determines which class' method to invoke.

This distinction is called hiding and overriding, further details of which are here:
Overriding and Hiding Methods
 
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

Originally posted by Karen Nelson:
... What disaster would happen if you were allowed to override a static method? ...


It's not a question of preventing disaster. It's simply how method "binding" works in Java.

See if this link from our SCJP FAQ helps.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am able to override a static method.why it not be override.
 
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sujittt Tripathyrr,


At one side you are telling that you are able to override a static method, and other side you are asking

"Why it not override"




Well I tell you, it is not like that, you can override static method, but it never seems to be overrided, because Static method only referred by Class only not by Object.

I can show you one example. even you can call static Method by a null object having that class Reference.

Check out this one:




This shows and proves that Static method cann't be logically overrided.

Hope It clears you.

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Additional stuff
 
Karen Nelson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to everybody who responded! I think I understand it better now -- especially, that I was approaching it from the wrong direction, trying to figure out what would be 'bad' about overriding a static method rather than thinking about the logic of the language.
-Karen.
reply
    Bookmark Topic Watch Topic
  • New Topic