In my opinion calling getCity()
method will return null, so null.name
makes no sense, but it works. Both print statements are printed.
package javaapplication1;
public class JavaApplication1 {
static String name = "New york";
static JavaApplication1 getCity(){
System.out.println("Getting city");
return null;
}
public static void main(String[] args) {
System.out.println( getCity().name );
}
}
Working example: https://ideone.com/rTJup5
We can access the static variable through class name as well as object reference. - In evaluation of `getCity().name` first `getCity()` is evaluated and result is discarded. – Pavan Apr 10 '18 at 12:04