This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static methods cannot be overridden Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods cannot be overridden" Watch "Static methods cannot be overridden" New topic
Author

Static methods cannot be overridden

Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Hello,
I read Corey's article which says that static methods CANNOT be overridden.So I tweaked the code as below.



So if the static method myStatic() in the child class is not overridden why do I get the following error

myStatic() in Child cannot override myStatic() in Parent;
overridden method is static final


I am confused or may be I am thinking wrong !!! Help!!!


(No need to shout)
[ September 30, 2004: Message edited by: Barry Gaunt ]

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
The question is why does the compiler treats the static method in the child class as OVERRIDDEN ? The JLS says static method in the child class hides the static method in Parent class.
So why doesn't it hide when the parent class static method is FINAL ?
Ozgur Nickpherly
Greenhorn

Joined: Sep 23, 2004
Posts: 3
First of all, error can be removed by deleting final keyword. Final functions can not be overrided whether they are object method or static method.
If object methods are overrided, you can not reach super method but static methods can be reached. See example below:

class Parent {
public static void myStatic() {
System.out.println("Parent");
}}
class Child extends Parent
{
public static void myStatic() {
System.out.println("Child");
}
public static void main(String[] args){
Parent p = new Child();
p.myStatic(); // prints Parent
Child c = (Child)p;
c.myStatic(); //prints Child
}}
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I have deleted my previous post because it was wrong.

Well here it is from the JLS:

8.4.3.3 final Methods
A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.



Notice the "or hiding". It does not matter if the method is static or not. Maybe the compiler implementor is reusing the error message used for overriding rather than using a specific error message for hiding.
[ September 30, 2004: Message edited by: Barry Gaunt ]

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

Joined: Aug 27, 2004
Posts: 411
Thank you Barry for that clarification. I was puzzled by the compiler's message.

Thank you to all who posted the message in regards to my query.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Yes, Barry is dead on. It's the compiler error that is so confusing. The error arises because you're trying to hide a final method, not override it. Static methods cannot be overridden, but they can be hidden. The keyword "final," however, prevents either from taking place.


SCJP Tipline, etc.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Static methods cannot be overridden
 
Similar Threads
Static methods cannot be overridden ?
Good question can you figure it out
Accessibility of private methods in inner classes
Polymorphism Problem
Static methods and inheritance.