Sub topic 5
Shadowing Between Inner and Outer Names
This chapter assumes you know instance fields, method parameters, access modifiers, and how to declare an inner class. The examples use a non-static inner class, which is an inner class tied to an object of its enclosing class.
What Shadowing Means
Name shadowing happens when a nearer declaration has the same name as a declaration in an outer scope. Java uses the nearest matching name when you write an unqualified name.
For an inner class, these scopes can contain the same name:
- A method parameter is nearest.
- A local variable is visible within its block.
- The inner class's field belongs to the inner object.
- The outer class's field belongs to the enclosing object.
Use a qualifier when you need a declaration from a less immediate scope.
`this` and `Outer.this`
Inside an inner class:
xmeans the nearest visiblex.this.xmeans the inner object's field namedx.Outer.this.xmeans the enclosing outer object's field namedx.
The name before .this must be the enclosing class name.
public class Shadow {
private int x = 1;
class Inner {
private int x = 2;
void show(int x) {
System.out.println(x); // 3: the parameter
System.out.println(this.x); // 2: Inner.x
System.out.println(Shadow.this.x); // 1: Shadow.x
}
}
public static void main(String[] args) {
Shadow outer = new Shadow();
Shadow.Inner inner = outer.new Inner();
inner.show(3);
}
}The output is:
3
2
1The parameter shadows both fields when the method refers to x directly. The inner field shadows the outer field when the method refers to this.x. The explicit Shadow.this.x reaches through the inner object to its enclosing Shadow object.
Common Errors
A frequent mistake is assuming this always means the outer object. Inside Inner, this means the current Inner instance. Use Shadow.this for the enclosing instance.
Another mistake is trying to use Outer.this from a static nested class. A static nested class has no enclosing object, so there is no outer instance to reference.
You can also accidentally hide a field while assigning a constructor parameter:
class User {
private String name;
User(String name) {
this.name = name;
}
}Here, name on the right is the parameter. this.name on the left is the field. Without this, the assignment would assign the parameter to itself.
Choosing Clear Names
Qualifiers solve ambiguity, but descriptive names often make code easier to read. For example, outerCount and innerCount communicate intent better than two fields both named count.
Use the same name when it expresses a useful relationship, such as a constructor parameter and a field. Use different names when multiple objects or scopes would otherwise be difficult to distinguish.
Exercise
Exercise
In an inner class with a field named value, a method parameter named value, and an outer field named value, which declarations do value, this.value, and Outer.this.value refer to?
Show answer
value refers to the method parameter, this.value refers to the inner-class field, and Outer.this.value refers to the enclosing object's field.