Code description and ouput
In the following code. We have a class TestInners, one inner class A, one method local inner class A and one outer class A.
- When we instantiate an object as in
new A().m();
the ouput is middle. - In order for the program to output inner we must instantiate the object after the method local inner class A in the gomethod.
- If we comment the inner class the program will output outer.
Question
In the code as it is. Why did it output middle? Is there a preference for the inner classes first? then the outer classes? I got confused.
Source code
class A { void m() { System.out.println("outer"); } }
public class TestInners {
public static void main(String[] args) {
new TestInners().go();
}
void go() {
new A().m();
class A { void m() { System.out.println("inner"); } }
}
class A { void m() { System.out.println("middle"); } }
}