aspose file tools*
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
Author

Compiler error overriding static methods

Greg George
Greenhorn

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)
{
try
{
ORTest.staticMethod();
Base.staticMethod();
}
catch (IOException e)
{
}
}

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

Joined: Oct 10, 2000
Posts: 1364
Because the Java Language Specification says so:

8.4.6.3 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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Compiler error overriding static methods