File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods cannot be overridden" Watch "Static methods cannot be overridden" New topic

Static methods cannot be overridden

Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
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

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() {
class Child extends Parent
public static void myStatic() {
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: 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.
I agree. Here's the link:
subject: Static methods cannot be overridden
jQuery in Action, 3rd edition