Sub topic 4
Outer.this and outer.new Inner()
Assume you know how to write classes, instance methods, constructors, and static members. This chapter focuses on non-static member classes, also called inner classes.
An inner class is a class declared inside another class without the static keyword. Each inner-class object belongs to one specific object of the enclosing class. The enclosing object is called the outer instance.
Creating an inner-class object
You create an inner-class object through an outer object:
public class Counter {
private int count;
public class Incrementer {
private final int step;
public Incrementer(int step) {
this.step = step;
}
public void run() {
count += step;
}
}
public int get() {
return count;
}
public static void main(String[] args) {
Counter counter = new Counter();
Counter.Incrementer incrementer =
counter.new Incrementer(2);
incrementer.run();
incrementer.run();
System.out.println(counter.get()); // 4
}
}The expression counter.new Incrementer(2) means:
- Use
counteras the enclosing object. - Create an
Incrementerobject attached to thatCounter. - Pass
2to the inner-class constructor.
The type name is Counter.Incrementer, but construction needs the particular outer object. A class name alone does not identify which Counter owns the new object.
Referring to the enclosing object
Inside an inner class, this means the current inner-class object. To refer to the enclosing object, use OuterClass.this.
public class Counter {
private int count;
public class Incrementer {
private final int step;
public Incrementer(int step) {
this.step = step;
}
public void run() {
Counter.this.count += step;
}
public Counter owner() {
return Counter.this;
}
}
public int get() {
return count;
}
public static void main(String[] args) {
Counter counter = new Counter();
Counter.Incrementer incrementer =
counter.new Incrementer(3);
incrementer.run();
System.out.println(counter.get()); // 3
System.out.println(incrementer.owner() == counter); // true
}
}Counter.this is useful when the inner and outer classes have fields or methods with the same name. It also lets the inner object return, compare, or pass around its owner.
> callout note > A non-static inner class cannot exist independently of an enclosing instance. A static nested class does not have an automatic outer-object link and is created with new Outer.Nested().
Resolving names
Java chooses an unqualified name from the nearest available scope. You can make the target explicit:
namerefers to the nearest local variable or parameter.this.namerefers to a field of the inner object.Outer.this.namerefers to a field of the enclosing object.
public class Shadow {
private int value = 1;
class Inner {
private int value = 2;
void show(int value) {
System.out.println(value); // 3: parameter
System.out.println(this.value); // 2: inner field
System.out.println(Shadow.this.value); // 1: outer field
}
}
public static void main(String[] args) {
new Shadow().new Inner().show(3);
}
}Use qualified names when the relationship matters. This avoids accidentally reading or changing a field from the wrong object.
When this relationship is useful
Inner classes fit designs where a helper needs direct access to one outer object’s state. Iterators, event handlers, and small implementation helpers are common examples. If the helper does not need an outer instance, prefer a static nested class. That choice makes the dependency explicit and avoids retaining an unnecessary outer object.
Exercise
How would you create an Inner object attached to an existing Outer object, and how would Inner refer to that exact Outer object from inside its code?
Show answer
Use Outer.Inner inner = outer.new Inner(); and refer to the enclosing object inside the class with Outer.this.