Does super has higher priority than outer class?
Consider we have three classes:
- ClassA
- ClassB
- Anonymous class in ClassB that extends ClassA
ClassA.java:
public class ClassA {
protected String var = "A Var";
public void foo() {
System.out.println("A foo()");
}
}
ClassB.java:
public class ClassB {
private String var = "B Var";
public void test() {
new ClassA() {
public void test() {
foo();
System.out.println(var);
}
}.test();
}
public void foo() {
System.out.println("B foo()");
}
}
When I call new ClassB().test(), I get the following output (which is pretty much expected):
A foo()
A Var
Question: Is it defined somewhere that inner class takes (methods and members) first from the super class and then from the outer class or is it JVM compiler implementation dependent? I have looked over the JLS(§15.12.3) but couldn’t find any reference for that, maybe it is pointed out there but I misunderstood some of the terms?
See 6.3.1 Shadowing Declarations:
Which may be interpreted as “the declaration of
foo(inherited fromClassA) shadows the declaration of any other methods namedfoothat are in an enclosing scope (ClassB) at the point wherefoooccurs, throughout the scope offoo.”Also relevant – section 15.12.1: