Local Inner Class in Java
A local class is declared inside a method (or any block), exactly like a local variable. It exists only within that block: nothing outside the method can see it or create it. It's the right tool when a helper class makes sense for one method and nowhere else. Since Java 16 you can also declare local records, enums and interfaces, which are handy for small, method-only data shapes.
A Helper That Lives in One Method
public class Marks {
static String report(String student, int[] marks, int passMark) {
class Subject {
final int score;
Subject(int score) { this.score = score; }
boolean passed() { return score >= passMark; }
}
int passed = 0;
for (int m : marks) {
if (new Subject(m).passed()) {
passed++;
}
}
record Result(String name, int passed, int total) { }
Result r = new Result(student, passed, marks.length);
return r.name() + " passed " + r.passed() + " of " + r.total();
}
public static void main(String[] args) {
System.out.println(report("Srihitha", new int[] {78, 32, 91, 40}, 35));
}
}Srihitha passed 3 of 4How it works
class Subjectis declared insidereport. Its scope starts at its declaration and ends at the method's closing brace.passed()usespassMark, a parameter of the enclosing method. Local classes can read local variables and parameters that are effectively final (never reassigned).record Result(...)is a local record (Java 16). Records get their own lesson, "Records in Java"; here it just bundles three values.- Scores 78, 91 and 40 pass a mark of 35; 32 doesn't.
What Local Classes Can Use
| From | Allowed? |
|---|---|
| Effectively final locals and parameters of the enclosing method | Yes (a copy is captured) |
| Locals that are reassigned anywhere | No: compile error |
| Instance fields of the outer object (if the method isn't static) | Yes |
| Static members of the outer class | Yes |
Access modifiers on the local class itself (public, private) | No: it's already local |
Local records, enums and interfaces are implicitly static: they can't capture local variables or the outer this, which keeps them simple.
Local Class, Anonymous Class or Lambda?
| Need | Choose |
|---|---|
| A one-off implementation of a single-method interface | A lambda |
| A one-off object of a class with several methods | An anonymous class |
| A named helper type used several times in one method | A local class |
| A small data bundle inside one method | A local record |
Common Mistakes
Reassigning a captured variable. Even a later passMark++ makes the earlier capture a compile error.
Putting private or public on a local class. Local classes take no access modifiers.
Using a local class from another method. It doesn't exist outside its block; move it up to the class level.
Practice
- Easy: Inside a method, declare a local record
Item(String name, long price), create three items and print the costliest one. - Harder: Write a method
static int countWords(String text, int minLength)with a local classWordthat knows whether it's at leastminLengthlong. Count the qualifying words in a sentence.
Key Takeaways
Was this tutorial helpful?