Reputation: 2441
In the following code. We have a class TestInners, one inner class A, one method local inner class A and one outer class A.
new A().m();
the ouput is
middle.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.
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"); } }
}
Upvotes: 1
Views: 621
Reputation: 262534
Yes, if you shadow symbols with more local definitions, the more local one is chosen. This most frequently happens with method parameters vs instance fields, leading to the famous this.name = name
idiom.
In your case, you can get to the outer class by using a fully qualified class name.
But don't name classes like that. Too much confusion for no reason.
Upvotes: 5