From the Java Language Specification
Receiver Variable Is Irrelevant For static Field Access
The following program demonstrates that a null reference can be used to access a class variable (static) without throwing an exception:
class Test3 { static String mountain = "Chocorua"; static Test3 favorite(){ System.out.print("Mount "); return null; } public static void main(String[] args) { System.out.println(favorite().mountain); } }
It compiles, executes, and prints:
Mount Chocorua
Even if the result of the favorite () function is null, a NullPointerException is not thrown. What Mount prints shows that the Primary expression is indeed fully evaluated at runtime, even though its type is used to determine which field needs access, not its value (since the field is static )
It is ignored, although the primary expression (Here is instance) is evaluated at run time, but its value is discarded and only its type is considered.
source share