Trail: Learning the Java Language
Lesson: Classes and Objects
Section: Nested Classes
When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions
Home Page > Learning the Java Language > Classes and Objects

When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions


Beta Draft 2013-09-10
This section was updated to reflect features and conventions of the upcoming Java SE 8 release. You can download the current JDK 8 snapshot from java.net.

As mentioned in the section Nested Classes, nested classes enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code. Local classes, anonymous classes, and lambda expressions also impart these advantages; however, they are intended to be used for more specific situations:


Problems with the examples? Try Compiling and Running the Examples: FAQs.
Complaints? Compliments? Suggestions? Give us your feedback.

Previous page: Method References
Next page: Questions and Exercises: Nested Classes