It's about the following situation:
public class A {
int x=3;
public A() {
setX(x-3);
}
void setX(int z) {
this.x = z;
}
}
public class B extends A {
static int x = 7;
void setX(int z) {
x = z;
}
}
public class Main {
public static void main(String[] args) {
A ab = new B();
System.out.println(B.x);
}
}
Output: 0
I'm already familiar with the fact, that the method of the subclass is executed in when we create an object that way.
Judging by the output, the method setX in class B takes the inherited x as argument, but has a sideeffect on the static variable. Is there a name for this behaviour or a more general explanation? Someone who doesn't know better could for example think, that the method takes the static variable as argument and has a sideffect on the inherited variable.