The below code produces the output middle. Can anyone explain in detail how this is happening?
Is it because the declaration of "inner" version of class A comes after the instance of class A is created in the go() method?
class A {
void m() {
System.out.println("outer");
}
}
public class MethodLocalVSInner {
public static void main(String[] args) {
new MethodLocalVSInner().go();
}
void go() {
new A().m();
class A {
void m() {
System.out.println("inner");
}
}
}
class A {
void m() {
System.out.println("middle");
}
}
}
AtoBand you'll getouteroutput. This should give you a hint.The type A is never used locally, That says local classAdefined inside methodgois never used.