Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arn't static methods overridden

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I know this Question is already discussed many times but this example is strange. Before trying this example i thought static methods r not overridden and i still have the same view, but with a doubt.
This example doesn't compile in its present form and gives a compile time error.
import java.io.*;
public class StaticException extends StaticExceptionBase{
public static void method() throws CloneNotSupportedException { }
}
class StaticExceptionBase {
public static void method() throws IOException { }
}
The Error message :
C:\Java\ex> javac StaticException.java
StaticException.java:4: The method void method() declared in class StaticException cannot override the method of the same signature declared in class StaticExceptionBase. Their throws clauses are incompatible.
public static void method() throws cloneNotSupportedException { }
^
1 error
It says the method cannot override coz the throws clause r different, thatz it. so if u change it it works. Doesn't that mean something strange. i mean doesn't it make u think that static methods r overridden.
import java.io.*;
public class StaticException extends StaticExceptionBase{
public static void method() throws IOException { }
}
class StaticExceptionBase {
public static void method() throws IOException { }
}
I request the moderators, if possible couldn't they ask someone from Sun Microsystems.
Thanks
Sanjeet
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty sure the problem you've encountered is because Java does not allow you to define exceptions of different classes to be thrown in overriding methods. There is no problem overriding static methods. In the second example, the throws clauses are identical so it will compile. If you were to switch the throws exceptions (assuming the one exception extends from IOException) it should compile.
From pg. 148 RHE, "...[overriding] methods cannot be declared as throwing checked exceptions of classes other than those that were declared by the original method."
Example:
import java.io.*;
public class StaticException extends StaticExceptionBase{
public static void method() throws EOFException { }
}
class StaticExceptionBase {
public static void method() throws IOException { }
}
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanjeet,
You are right. Static methods are never overridden, but they are hidden.
Read the section 8.4.4 of JLS. It says

A method that overrides or hides another method (�8.4.6), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.

Clearly the rules of declaring exceptions in the throws clause includes the hiding scenario that happens when you redeclare static method with the same signature in the subclass.
Hope this helps,
Ajith
 
Let me tell you a story about a man named Jed. He made this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic