File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help on static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help on static method" Watch "Help on static method" New topic

Help on static method

HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
I hope my understanding so far has been correct.
Static method cannot be override.
Thus, when a extendedclass extends a base class with a same name and argument list, which method to use is resolved at compile time using the reference type it is declared as.
Base b = new Extended();
b.staticprintout(); // calls the Base static method
Extended b = new Extended();
b.staticprintout(); // calls the Extended static method
I'd like to know for the following code, why does the extended class object b managed to resovle and find Base staticprintout.
public class Base
public static void staticprintout()
System.out.println("Base static printout");
public static void main(String args[])
Extended b = new Extended();
class Extended extends Base
Thanks for your help.
Han Ming
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
Since there is nothing hiding the Base static method, the subClass can use the method it inherits from Base.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
1.static methods can not be overridden, but they are accessible to derived class and other class(in case of public).
2. static methods can be accessed using object refernces. but to make things clear , it is adivised to access ctatic method using class name.
So when u are accessing static method of Base class using object refernce of extended class, it is available to that reference as it is derived from the Base class like any other method.

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
This is super quick response.
THANKS so much for the response.
Hmm, I think I have understand it better.

Han Ming
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
1 more point.
static menthod can't be overridden but you can hide them
in subclasses.
I agree. Here's the link:
subject: Help on static method
It's not a secret anymore!