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 redefined
What does redefining actually mean? Is it a case of having a static method that exists in both parent and child, with the same signature, however they are referenced separately by their class names? Such as :
class Parent
{
static void doSomething(String s){};
}
class Child extends Parent
{
static void doSomething(String s){};
}
Referenced as : Parent.doSomething(); and Child.doSomething(); ?
Also, does the same apply for static variables, or just static methods?
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, thedoSomethingmethod of the Parent is invoked:If the methods were non-static,
doSomethingof Child would override that of Parent andchild.doSomethingwould have been invoked.The same holds for static fields.