• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

static methods

 
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remember things better if I understand the underlying reason for the behavior. That said, can anyone explain why static methods cannot be overridden?

Thanks.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Higgledy Smith wrote:I remember things better if I understand the underlying reason for the behavior. That said, can anyone explain why static methods cannot be overridden?

Thanks.


Static methods can be hidden (but not overridden.) Why can't they be overridden? Because overriding uses polymorphism (which calls the correct method based on the actual type of the instance referred to by the reference.) But with static methods you have no underlying instance, since they are linked to the class as a whole. That's why you can't override a static method (but if you declare a static method with the same signature in a subclass, that static method will hide the superclass' static method in subclass code.)
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is worth noting that static methods cannot be abstract.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

victor kamat wrote:It is worth noting that static methods cannot be abstract.


You are correct. Abstract methods are meant to be overridden, which static methods can not. Surprisingly, however, static methods can be marked final. A static method which is marked final can not be redefined in a subclass.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic