问题
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?
回答1:
See 6.3.1 Shadowing Declarations:
A declaration d of a method named n shadows the declarations of any other methods named n that are in an enclosing scope at the point where d occurs throughout the scope of d.
Which may be interpreted as "the declaration of foo
(inherited from ClassA
) shadows the declaration of any other methods named foo
that are in an enclosing scope (ClassB
) at the point where foo
occurs, throughout the scope of foo
."
Also relevant - section 15.12.1:
15.12.1 Compile-Time Step 1: Determine Class or Interface to Search
The first step in processing a method invocation at compile time is to figure out the name of the method to be invoked and which class or interface to check for definitions of methods of that name. There are several cases to consider, depending on the form that precedes the left parenthesis, as follows:
- If the form is MethodName, then there are three subcases:
- If it is a simple name, that is, just an Identifier, then the name of the method is the Identifier. If the Identifier appears within the scope (§6.3) of a visible method declaration with that name, then there must be an enclosing type declaration of which that method is a member. Let T be the innermost such type declaration. The class or interface to search is T.
- If it is a qualified name of the form TypeName.Identifier, then [...]
- In all other cases, the qualified name has the form FieldName.Identifier; then [...]
回答2:
I think you are always going to get "A var"
.
This is because your test()
method implementation is being defined on an anonymous subclass of A
. I don't think you can access the B.var
instance variable within your test()
method unless you explicitly refer to the outer class using ClassB.this.var
.
来源:https://stackoverflow.com/questions/5867287/outer-vs-super-class