What does redefining static methods mean in Java?

断了今生、忘了曾经 提交于 2019-12-03 05:47:21

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.

rajah9

Static means there is one per class, rather than one per object. This is true for both methods and variables.

A static field would imply that there is one such field, no matter how many objects of that class are created. Please take a look at Is there a way to override class variables in Java? for the question of overriding a static field. In short: a static field cannot be overridden.

Consider this:

public class Parent {
 static int key = 3;

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

public class Child extends Parent {

 static int key = 33;

 public static void main(String[] args) {
  Parent x = new Parent(); 
  x.getKey();
  Child y = new Child();
  y.getKey();
  Parent z = new Child();
  z.getKey();
 }
}

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

Key never comes back as 33. However, if you override getKey and add this to Child, then the results will be different.

@Override public void getKey() {
 System.out.println("I am in " + this.getClass() + " and my key is " + key);
}
I am in class tools.Parent and my key is 3
I am in class tools.Child and my key is 33
I am in class tools.Child and my key is 33

By overriding the getKey method, you are able to access the Child's static key.

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!