Originally posted by Prathamesh Gaddam:
How does the static memeber variable behaves towards inheritance?
Until a subclass hides it, a static field or method is available through the class itself, its subclasses and instances of all those classes. However, it is also available through null references! All that matters for those is the class the reference is declared as:
Although x is null, it is declared as being of class X, so x.A actually executes X.A.
That code also shows something that has been allowed in previous versions of
Java: although the array index is invalid, Java 1.3 (I believe) allowed it and would print 5 - since only the declared type (X) was used.
This is no longer possible though. Now, if a subclass of X called Y would also declare a static field called A, then the declared type is what matters: