aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help on static method" Watch "Help on static method" New topic
Author

Help on static method

HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
Hi,
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.
e.g.
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();
b.staticprintout();
}
}
class Extended extends Base
{}
****************************************************************
Thanks for your help.
Han Ming
Marilyn de Queiroz
Sheriff

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

JavaBeginnersFaq
"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: 5371
Hi
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.
CMIW
------------------
Regards
Ravish


"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
Hi,
This is super quick response.
THANKS so much for the response.
Hmm, I think I have understand it better.
Cheers.

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: http://aspose.com/file-tools
 
subject: Help on static method