Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static method overriding static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static method overriding static method" Watch "static method overriding static method" New topic
Author

static method overriding static method

Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
An answer in the study guide by Heller and Roberts says that "static methods cannot override static methods" but the following code works just fine. Any thoughts?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Have you tried:

?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Hi Dan, Barry,
I tried compiling with
H h = new G();
h.m();
It is giving an error saying h is already defined in Class4.
But I did find something in API documentation.
http://java.sun.com/docs/books/tutorial/java/javaOO/override.html
It says :
Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method.
A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass
What does it mean when they say hiding a static method with the same signature.
Pallavi
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Static methods do not participate in polymorphism therefore they are hidden not overridden.
This is one place where C# has Java beat. In C# you can't run static methods (which are class methods) with an object reference.
Think about how we normally execute static methods. We don't do:
H h = new H();
h.m();
we do:
H.m();
Try this: the method that gets executed is determined at compile time for static methods. So when you do something like:
H h = new G();
h.m();
Since h is a pointer to an H object, it is the static method of the H class that will be executed, not the static method of the G class. That is because at compile time the compiler has no idea that h is pointing to a G object.
Static methods don't participate in polymorphism therefore they can't be overridden. They can only be hidden.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
Good point, thank you all.
I guess Thomas is right, if you couldn't call a static method using an object reference this would not be confusing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static method overriding static method