java - Signature difference when hiding static method in a subclass -


recently playing around simple java code using main methods test code wrote. ended in situation had 2 classes similar those:

public class {     public static void main(string[] args) {         // code here     } }    public class b extends {     public static void main(string[] args) throws ioexception {         // code here     } } 

i quite surprised code stopped compiling , eclipse complained exception ioexception not compatible throws clause in a.main(string[]).

well, both methods static , main function in b hiding 1 a, thought there no relation between them. in static methods have no polymorphism , call bound concrete method implementation during compilation, therefore cannot understand why main in b cannot throw exception not declared in main signature in a.

why java designers decided enforce constraint , in situations cause problems if constraint not enforced compiler?

for it's worth, here relevant portion of jls enforces rule.

first, §8.4.8.2. hiding (by class methods) gives definition method hiding applies here:

if class c declares or inherits static method m, m said hide method m', signature of m subsignature (§8.4.2) of signature of m', in superclasses , superinterfaces of c otherwise accessible code in c.

then, §8.4.8.3. requirements in overriding , hiding states that:

a method overrides or hides method, including methods implement abstract methods defined in interfaces, may not declared throw more checked exceptions overridden or hidden method.

more precisely, suppose b class or interface, , superclass or superinterface of b, , method declaration m2 in b overrides or hides method declaration m1 in a. then:

  • if m2 has throws clause mentions checked exception types, m1 must have throws clause, or compile-time error occurs.

  • for every checked exception type listed in throws clause of m2, same exception class or 1 of supertypes must occur in erasure (§4.6) of throws clause of m1; otherwise, compile-time error occurs.

in other words, error message not oversight in compiler, or misinterpretation of spec; jls makes specific effort mention throws clause conflicts are error method hiding (i.e., static methods). there equivalent language in every version of jls 1.0.

however, can't definitively answer question of why constraint present in case. can't conceive of situation in constraint necessary, since issue of static method implementation invoked resolved @ compile-time, unlike instance methods.

i'd bet small amount of money whoever first put constraint in langspec being over-cautious, figuring safer prevent allow , later discover causes problems. java language design is/was not without fair share of flawed features (checked exceptions being 1 of them), , credibly another, guess.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -