I have the following code:
class SuperClass { public static String getName() { return 'super'; } } class SubClass extends SuperClass { public static String getName() { return 'sub'; } } public class Dummy<T extends SuperClass> { public void print() { System.out.println('SuperClass: ' + SuperClass.getName()); System.out.println('SubClass: ' + SubClass.getName()); System.out.println('T: ' + T.getName()); } public static void main(String[] args) { new Dummy<SubClass>().print(); } }
This code outputs the following:
SuperClass: super SubClass: sub T: super
My question is: Why doesn’t T.getName() return the value of SubClass.getName()? After all, I specified that T == SubClass. Or are static function calls invalid for generic references?
Thanks a lot in advance!
This isn’t just an issue about generics.
If you say:
you will also get ‘super’. There are no ‘polymorphic’ static methods.
In your case, all the compiler knows about
Tis that it extendsSuperClass, so it will callSuperClass.getName().