• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help on static method

 
HanMing Low
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since there is nothing hiding the Base static method, the subClass can use the method it inherits from Base.
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
HanMing Low
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 more point.
static menthod can't be overridden but you can hide them
in subclasses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic