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
startmethod is called. - The
listeneris initialized. - The
listenerreference is passed to theTimerconstructor. At this point, thebeepparameter variable of thestartmethod no longer exists. - Moment later, the
actionPerformedmethod 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...