• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Methods  RSS feed

 
Vineela Devi
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class StaticTest
{static void method(){
System.out.println("Parent Method Called ");
}
public static void main(String[] args)
{StaticTest s = new StaticTest1();
s.method();
}
}
class StaticTest1 extends StaticTest{
static void method(){
System.out.println("Child Method called");
}
}
I was expecting an output of "child method called" because of late binding.
But the output is "Parent Method Called". Can anyone please explain how the above code works?
Thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods aren't polymorphic. They're statically bound at compile time. Historically, you could call a static method on a null object reference, because only the compile-time type of the reference counted (I understand that recent JVMs disallow this.)
If your next question is "why," I think there have been plenty of discussions on this very topic here in the Saloon; you could search, or I'm sure someone can come up with a link.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!