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

overriding of static methods

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am bit ( a lot actually !) confused on the concept of overriding static methods. for eg in the code

why is the output
Super class static method being called
Sub class static method being called
Super class static method being called
what is meant by the method in the subclass "shadows" the one in the superclass ?
thanx
[ Jess added UBB [CODE] tags to preserve whitespace, check them out! ]
[ August 15, 2002: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot truly "override" a static method, you can only "hide" or "shadow" the one declared in the superclass.
The decision of which static method to call is made at compile time, not runtime. It depends solely on the declared type of the variable, not on the actual type of the Object that the variable points to.
So, assuming that foo() is declared as a static method...
Thing t;
t = new RedThing();
t.foo();
This is equivalent to calling Thing.foo() . Even if RedThing has its own static foo() method, it won't be called.
Thing t does not even have to be a real Thing. This also works:
Thing t = null;
t.foo();
It calls Thing.foo() and does NOT cause a NullPointerException.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic