I am learning the 'Core Java' book about how the local inner class access variables from outer methods. While, I am confused about something.
The book provides a snippet of code here:
public void start(int interval, boolean beep)
{
class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("At the tone, the time is " + new Date());
if (beep) Toolkit.getDefaultToolkit().beep();
}
}
ActionListener listener = new TimePrinter();
Timer t = new Timer(interval, listener);
t.start();
}
The book illustrate how the flow of control here as:
- The
start
method is called. - The
listener
is initialized. - The
listener
reference is passed to theTimer
constructor. At this point, thebeep
parameter variable of thestart
method no longer exists. - Moment later, the
actionPerformed
method executesif (beep) ...
I am just confused about why the beep no longer exists? I thought the outer start method is still not coming to the end }, the local variable could still live...