What does redefining static methods mean in Java?

前端 未结 3 523
慢半拍i
慢半拍i 2021-02-08 01:08

I\'ve been reading a section on Statics in the SCJP study guide, and it mentions the following :

static methods can\'t be overridden, but they can be r

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-08 01:41

    It simply means that the functions are not virtual. As an example, say that you have an object of (runtime) type Child which is referenced from a variable of type Parent. Then if you invoke doSomething, the doSomething method of the Parent is invoked:

    Parent p = new Child();
    p.doSomething(); //Invokes Parent.doSomething
    

    If the methods were non-static, doSomething of Child would override that of Parent and child.doSomething would have been invoked.

    The same holds for static fields.

提交回复
热议问题