-
Inner classes are classes defined inside a class or even inside a method.
-
An inner class defined in a method can only access local variables of the method that are declared final.
-
An inner class defined inside a class can access the members of the containing class.
If a class has a member which needs a controller, it is convenient to create the controller as an inner class so that the controller methods will have easy access to the member.
-
A controller class doesn't have to be an inner class, but it would need to have access to the object it will control.
For example, the RectangleComponentFrame could add a controller for its RectangleComponent member by passing the component to the controller's constructor:
// Create a mouse listener for this component MouseListener listener = new MousePressedListener(component); component.addMouseListener(listener);
where MousePressedListener is an ordinary class (not an innner class) that implements the MouseListener interface:
public class MousePressedListener extends MouseAdapter { RectangleComponent component; public MousePressedListener(RectangleComponent component) { this.component = component; } public void mousePressed(MouseEvent event) { int x = event.getX(); int y = event.getY(); component.moveTo(x, y); } }