wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Compiler error overriding static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiler error overriding static methods" Watch "Compiler error overriding static methods" New topic

Compiler error overriding static methods

Greg George

Joined: May 14, 2003
Posts: 1
I understand that you can't override static methods. But then why does the compiler generate the following error when attempting to compile the code shown? If there is no overriding going on then the thrown exception should be irrelevant. Am I missing something here?

> ORTest.java:21: staticMethod() in ORTest cannot override staticMethod() in Base;
> overridden method does not throw java.io.IOException
> public static void staticMethod() throws IOException
> ^
> 1 error
import java.io.*;

class Base
public static void staticMethod()
System.out.println("In Base.staticMethod()");

public class ORTest extends Base
public static void main(String[] args)
catch (IOException e)

public static void staticMethod() throws IOException
System.out.println("In ORTest.staticMethod()");
Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Because the Java Language Specification says so: Requirements in Overriding and Hiding
Moreover, a method declaration must not have a throws clause that conflicts (�8.4.4) with that of any method that it overrides or hides; otherwise, a compile-time error occurs.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The only practical difference between instance method overriding and class method hiding is that instance method calls select the method to invoke based on run time polymorphism.

Mike Gershman
SCJP 1.4, SCWCD in process
I agree. Here's the link: http://aspose.com/file-tools
subject: Compiler error overriding static methods
Similar Threads
Please clarify my doubts about static methods
difference between static and final method
Overriding a static Method
Should shadowed static method follow overriden method rules ?
static methods can't be overidden why