I am confused with be below behaviour in Java, I know that static inheritance is not possible Java then how I am able to call on B class static members of A?
public class A {
static int staticVariable = 5;
public static void staticMethod(){
System.out.println("A");
}
}
public class B extends A{}
public class C {
public static void main(String[] args) {
A.staticMethod();
B.staticMethod();
System.out.printf("A's int value %d and B's int value is %d",A.staticVariable,B.staticVariable);
}
}
output:
A
A
A's int value 5 and B's int value is 5
What you do here is not “static inheritance” whatever that means, but plain old name resolution (at compile time). This works so that you can qualify a name with B everywhere you would put an A, except for names that are defined in B itself.