Sub topic 6
Local Classes Declared Inside Methods
Assume you can write and compile basic Java classes, use interfaces, and distinguish instance variables from local variables. This chapter focuses on local classes, which are classes declared inside a method or another block.
A local class is useful when a helper type is needed only by one method. Its name is visible only inside the block where the declaration appears. This keeps implementation details out of the surrounding class's public API.
Declaring a local class
A local class can have fields, constructors, methods, and implemented interfaces. Declare it before using it in the method.
public class Greeters {
static Runnable makeGreeter(String name) {
class Greeter implements Runnable {
@Override
public void run() {
System.out.println("Hello, " + name);
}
}
return new Greeter();
}
public static void main(String[] args) {
Runnable greeter = makeGreeter("Ada");
greeter.run();
}
}Greeter exists only inside makeGreeter. Code in main cannot write new Greeter(). It receives the object through the Runnable interface instead.
The class can use name, which is a parameter of the enclosing method. The returned object may continue using that value after makeGreeter has finished. Java therefore captures the value for the object.
Capturing local variables
A local class may read a local variable or parameter only when that variable is final or effectively final. Effectively final means that the variable is assigned once and never assigned again.
public class NumberPrinter {
static Runnable makePrinter(int start) {
int amount = start + 1; // effectively final
class Printer implements Runnable {
@Override
public void run() {
System.out.println("Amount: " + amount);
}
}
return new Printer();
}
public static void main(String[] args) {
makePrinter(4).run(); // Amount: 5
}
}This code does not compile if amount is changed after its initialization:
amount++;The restriction prevents confusing behavior. The local variable belongs to the method call, while the local-class object can live longer than that call. Java captures the variable's value, not a freely changing method-local storage location.
A local class can access instance fields through the enclosing object when the method is an instance method. It can also access private members of the enclosing class.
Scope and lifetime
The local class declaration must appear inside the method or block where it is used. Its class name is not visible outside that region, but an instance can escape through a return value, a field, or an interface type.
A local class declared in a static method cannot use this for an enclosing instance because no enclosing object exists. A local class declared in an instance method can read the surrounding object's instance members.
Choosing a local class
Choose a local class when all of these conditions fit:
- The helper is meaningful in only one method or block.
- The helper needs named methods or its own fields.
- Returning it through an interface or superclass is acceptable.
- Captured local variables will remain effectively final.
If several methods need the helper, make it a member class instead. If the helper implements one functional interface and needs no extra state or inheritance, consider a lambda.
Exercise
Why does makeGreeter("Ada").run() work even though Greeter is not visible outside makeGreeter?
Show answer
The method returns the object through the visible Runnable interface. The caller needs only that interface, not the local class's name.