my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding of static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding of static methods" Watch "overriding of static methods" New topic

overriding of static methods

Soum Sark
Ranch Hand

Joined: May 31, 2002
Posts: 50
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 ?
[ Jess added UBB [CODE] tags to preserve whitespace, check them out! ]
[ August 15, 2002: Message edited by: Jessica Sant ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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();;
This is equivalent to calling . 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;;
It calls and does NOT cause a NullPointerException.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
I agree. Here's the link:
subject: overriding of static methods
It's not a secret anymore!