What does redefining static methods mean in Java?

前端 未结 3 525
慢半拍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:43

    In rajah9's answer if now we make the two methods static both in parent and child:

    public static void getKey() {
            System.out.println("I am in and my key is " + key);
        }
    

    Two things to note now: Can't use 'this.getClass()' and warning 'The static method getKey() from the type Parent should be accessed in a static way' also

    Parent z = new Child();
      z.getKey();
    

    Will give the output

    I am in class tools.Parent and my key is 3
    

    instead of

    I am in class tools.Parent and my key is 33
    

提交回复
热议问题