According to http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods%28%29 , Class.getDeclaredMethods () should only include methods declared by the class. However, I get some pretty unexpected results with the test code below:
import java.util.Arrays; class A { public static A m1() { return null; } public static A m2() { return null; } public static A m3() { return null; } } class B extends A { public static A m1() { return null; } public static B m2() { return null; }
A few things that seem strange / unexpected to me:
The compiler complains about B.m3 () having an incompatible return type with A.m3 (). This is dictated by JSL 8.4.8.3 (Page 225 v3 langspec-3.0.pdf). But I'm curious why this restriction needs to be applied to static methods. My understanding is that static methods can be hidden and not overridden, and that the link is allowed at compile time, so what explains this limitation?
The output includes two m2 () methods. This contradicts the assertion that getDeclaredMethods () returns only the methods declared by the class and "excludes inherited methods." I do not see A.m2 () as "declared" B. "
The output includes one m1 (). Following the previous paragraph, if it makes sense to let getDeclaredMethods () return two m2 (), why doesn't it return two m1 ()? They are still two different methods, one is hidden from the other. I see no fundamental difference between the case of m1 () and m2 (), except that the return type is the same in the case of m1 (), but the return type, as I understand it, is not part of the method signature?
Thanks in advance!
java reflection static static-methods method-hiding
Ray
source share