Sub topic 3
Inner Classes and Their Link to an Outer Object
This chapter assumes that you can write basic Java classes, use instance and static members, understand access modifiers, and implement interfaces or abstract classes.
An inner class is a non-static class declared inside another class. Each inner-class object is connected to one object of the enclosing, or outer, class. This connection lets the inner object use the outer object's instance fields and methods directly.
The outer instance link
Consider a counter with an inner Incrementer class:
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 Counter owner() {
return Counter.this;
}
}
public int get() {
return count;
}
public static void main(String[] args) {
Counter c = new Counter();
Counter.Incrementer inc = c.new Incrementer(2);
inc.run();
inc.run();
System.out.println(c.get()); // 4
System.out.println(inc.owner() == c); // true
}
}Incrementer is an inner class because it is not marked static. The expression c.new Incrementer(2) creates an Incrementer that belongs to c.
Inside Incrementer, the statement count += step means that the object should update the count field of its associated Counter. Java lets an inner class access private members of its enclosing class.
The type name is Counter.Incrementer, but creating an instance requires an outer object:
Counter.Incrementer inc = c.new Incrementer(2);The outer object must exist first. An inner object cannot be created independently with new Counter.Incrementer(2).
Referring to the enclosing object
Within an ordinary instance method, this means the current inner-class object. To refer to the associated outer object, use OuterClass.this.
public Counter owner() {
return Counter.this;
}This form is useful when the inner and outer classes both have methods or fields with similar names. It also makes the relationship explicit when passing the outer object to another method.
The link can be pictured as follows:
Multiple outer objects
Each inner object is tied to the outer object used during construction. Two outer objects can therefore have separate inner objects with separate state:
Counter first = new Counter();
Counter second = new Counter();
Counter.Incrementer firstInc = first.new Incrementer(1);
Counter.Incrementer secondInc = second.new Incrementer(10);
firstInc.run();
secondInc.run();
System.out.println(first.get()); // 1
System.out.println(second.get()); // 10Although both variables have the same type, firstInc changes first, while secondInc changes second.
A static nested class does not have this automatic link. It can be created through the class name, such as new Counter.Helper(), and it cannot directly use Counter instance fields. Use an inner class when the nested object's behavior depends on a particular outer object.
Common errors
- Marking the class
staticand then trying to access an outer instance field. - Writing
new Counter.Incrementer(...)without an outer object. - Using
thiswhenCounter.thisis required. - Assuming that all
Counter.Incrementerobjects share oneCounter. Each one belongs to the outer object used in its construction.
Exercise
How would you create an Incrementer with a step of 5 for a Counter variable named counter?
Show answer
Write Counter.Incrementer inc = counter.new Incrementer(5);.