In the java example below, can anyone explain exactly why the output of the program is “Orange” ? (this is an interview question)
public class Finder {
public static void main(String[] args){
System.out.println(X.Y.Z);
}
}
class X {
static W Y = new W();
static class Y {
static String Z ="Apple";
}
}
class W {
String Z = "Orange";
}
The variable Y obscures the type Y. See the JLS:
The qualified name X.Y.Z is resolved according to:
This is unlikely to occur in practice because of the normal naming conventions for types and variables.