Java Reference
In-Depth Information
Close All
mail window
window 2
window 1
window 3
FIGURE 10.3: Example of multicasting.
there will be three objects of type ActionListener that are registered with the menu item
closeAll , one for each window. The actionPerformed method for every window simply
closes the window.
Note that the variable closeAll inside the MyFrame constructor had to be defined as
final . The reason is that it is accessed from an anonymous local class. The final keyword
means that the variable will not be assigned a new value in the method.
10.5 Summary
The chapter describes how to use the Timer class. It allows us to execute a method
periodically. This method is executed in parallel with the rest of the program. A timer is
implemented by creating a separate thread, but multithreading is an advanced subject that
is not covered in this topic.
The chapter also describes how to create nested classes. There are four types of nested
classes: static, inner, local, and anonymous local. The first two types allow us to create a
class within a class, while the last two types allow us to create a class within a method. A
local class that is not anonymous is rarely useful and not covered in detail in the chapter.
Static classes are associated only with the outer class, while inner classes are associated
with an instance of the outer class. In this regard, there is a similarity between inner classes
and subclasses. In both cases, there is a hidden reference to an outside object.
Nested classes are useful to create method callbacks. Note that an event listener object
must be created every time we want to handle an event. The reason is that we cannot
directly tell Java to execute a method every time an event occurs. Instead, we need to
create an object (called an event listener) and register it with the source of the event. Every
time the event occurs, the event listener is notified by calling one of its methods. The event
is passed as a parameter when calling the method.
The chapter also covers multicasting. Multicasting occurs when several event listeners
are registered with the same event source. This is a powerful mechanism for handling events
because a single event can trigger multiple methods.
Search WWH ::

Custom Search