CKTIS / Java
Inner classes and `outer.new Inner(...)`
An inner member class is tied to a specific instance of its enclosing class and can read that object's private fields.
Version: Java SE 21
Syntax
class Outer {
class Inner {
Inner(int value) { /* initialize this Inner object */ }
}
Inner create(int value) {
return new Inner(value); // enclosing instance: this Outer
}
}
// In a method or initializer where the type and constructor are accessible:
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner(42);In Java, an inner class is a nested class that is neither explicitly nor implicitly static. Non-static local classes and anonymous classes also qualify. If such a local or anonymous class is declared in a static context, its objects have no immediately enclosing instance. By contrast, an inner member class such as Team.Member is bound to a particular enclosing object when instantiated.
In outer.new Inner(...), outer supplies the enclosing object, and the arguments in parentheses go to the Inner constructor. Outer.Inner names the type, not the enclosing object. In an instance method of Outer, new Inner(...) can use this as the implicit enclosing object. In a static context, omitting the qualifier is a compile-time error; a qualifier that evaluates to null causes NullPointerException at run time. The type and constructor must still be accessible.
In an instance method of Member, this denotes the Member object, while Team.this denotes its enclosing Team object. Team.this.name reads the team's private field even though Member has its own name. A static nested class has no implicit enclosing object: it needs an explicit reference to read that object's instance fields. In Java SE 21, an inner class can declare static members and static initializers without becoming a static nested class.
TaskList declares FilteringIterator as a private final member class. The positiveIterator() method creates an instance of that class and returns it as Iterator<Integer>. Each iterator has its own index and reads the enclosing list's source. It skips non-positive values, and next() throws NoSuchElementException once the iterator is exhausted. Keeping the iterator can keep its enclosing TaskList alive.
Examples
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Main {
public static void main(String[] args) {
Team red = new Team("Red");
Team blue = new Team("Blue");
Team.Member first = red.new Member("Ada");
Team.Member second = blue.new Member("Ada");
System.out.println(first.label());
System.out.println(second.label());
TaskList tasks = new TaskList(0, 4, -1, 7);
Iterator<Integer> positives = tasks.positiveIterator();
while (positives.hasNext()) {
System.out.println(positives.next());
}
}
}
final class Team {
private final String name;
Team(String name) {
this.name = name;
}
class Member {
private final String name;
Member(String name) {
this.name = name;
}
String label() {
return Team.this.name + ": " + this.name;
}
}
}
final class TaskList {
private final int[] source;
TaskList(int... values) {
source = values.clone();
}
Iterator<Integer> positiveIterator() {
return new FilteringIterator();
}
private final class FilteringIterator implements Iterator<Integer> {
private int index;
@Override
public boolean hasNext() {
while (index < source.length && source[index] <= 0) {
index++;
}
return index < source.length;
}
@Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return source[index++];
}
}
}Result
Red: Ada Blue: Ada 4 7
The two `Member` objects have the same name but print different team names. The iterator then prints only `4` and `7`, in their order in `source`.
Related lessons
- Java · Lesson 7 · Intermediate · Version: 2
- Java · Lesson 24 · Intermediate · Version: 2
Sources
- Java Language Specification, Java SE 21, §8.1.3 — Inner Classes and Enclosing Instances
- Java Language Specification, Java SE 21, §15.9 — Class Instance Creation Expressions
- Java Language Specification, Java SE 21, §15.8.4 — Qualified this
- Java Language Specification, Java SE 21, §6.6.1 — Determining Accessibility
- Iterator (Java SE 21 & JDK 21)