Regular Inner Classes

Recall that you can write many different classes in one source file. You can also write a class inside of another class.

Why in the world would you want to do that? Well, they sort of allow you to scope your classes. The inner class becomes a member of the outer class. That is, the inner class acts as a member of the outer class, just like its methods and fields.

Important thing: Objects in an inner class have access to even the private variables in instances of the outer class.

You typically use an inner class to define a helper—that is, a class with a very specific purpose. In GUI development, you need to create listeners that handle events such as mouse clicks. Often, these events are handled using inner classes.

There ...

Get Java Garage now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.