• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Compiler error overriding static methods

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()");
}
}
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic