References

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.Timer;

publicclass InnerClassTest

{

publicstaticvoid main(String[] args)

{

TalkingClock clock =new TalkingClock(1000,true);

clock.start();

// keep program running until user selects "Ok"

JOptionPane.showMessageDialog(null,"Quit program?");

System.exit(0);

}

}

/**

A clock that prints the time in regular intervals.

*/

class TalkingClock

{

/**

Constructs a talking clock

@param interval the interval between messages (in milliseconds)

@param beep true if the clock should beep

*/

public TalkingClock(int interval,boolean beep)

{

this.interval = interval;

this.beep = beep;

}

/**

Starts the clock.

*/

publicvoid start()

{

ActionListener listener =new TimePrinter();

Timer t =new Timer(interval, listener);

t.start();

}

privateint interval;

privateboolean beep;

privateclass TimePrinterimplements ActionListener

{

publicvoid actionPerformed(ActionEvent event)

{

Date now =new Date();

System.out.println("At the tone, the time is " + now);

if (beep) Toolkit.getDefaultToolkit().beep();

}

}

}

In the above program in the start method of class class TalkingClock the reference t for the timer is destroyed as and when the start method is finished right so how is the program working...

[3189 byte] By [crash_overridea] at [2007-11-27 9:36:35]
# 1

> In the above program in the start method of class

> class TalkingClock the reference t for the timer is

> destroyed as and when the start method is finished

> right so how is the program working...

The reference is destroyed but the object it pointed to is not. The Timer will use a thread which will keep a reference (directly or indirectly) to the timer.

sabre150a at 2007-7-12 23:06:00 > top of Java-index,Java Essentials,Java Programming...