Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • 2
    Intuitively, I would think it should work just like a virtual function. If B extends A and both A and B have virtual functions named doStuff, the compiler knows that instances of A should use A.doStuff and instances of B should use B.doStuff. Why can't it do the same with static functions? After all, the compiler knows what class each object is an instance of. Commented Feb 8, 2010 at 20:35
  • Erm ... Jay, a static method need not be (generally is not) called on an instance ... Commented Feb 8, 2010 at 22:06
  • 2
    @meriton, but then it's even easier, no? If a static method is called using a classname, you'd use the method appropriate for the class. Commented Feb 8, 2010 at 22:37
  • 1
    But then what is overriding doing for you. If you call A.doStuff() should it use the version that was overridden in "B extends A" or the version that was overridden in "C extends A". And if you have C or B then you are calling those versions anyway... no overriding necessary. Commented Feb 9, 2010 at 8:01
  • 1
    @Jay: No, that is not true. The compiler will generate a different type of call. For all virtual method calls, the compiler puts a reference to this on the stack. With a static method call (even, unfortunately with Java's this.staticMethod()) there is no hidden reference so the compiler really strips off this. from that call. Commented Mar 4, 2010 at 14:51