Previous | Next | Trail Map | Writing Applets | Overview of Applets


Methods for Milestones

public class Simple extends java.applet.Applet {
    . . .
    public void init() { . . . }
    public void start() { . . . }
    public void stop() { . . . }
    public void destroy() { . . . }
    . . .
}
The Simple applet, like every other applet, is a subclass of the Applet class. The Simple applet overrides four Applet methods so that it can respond to major events:
init()
to initialize the applet each time it's loaded (or reloaded)
start()
to start the applet's execution, such as when the applet's loaded or when the user revisits a page that contains the applet
stop()
to stop the applet's execution, such as when the user leaves the applet's page or quits the browser
destroy()
to perform a final cleanup in preparation for unloading
Not every applet needs to override all four of these methods. For example, the "Hello World" applet doesn't override start(), stop(), or destroy(), since it doesn't do anything after it's initialized. The "Hello World" applet just displays a string once, using its init() method. Most applets, however, do more.

Every applet that does something after initialization -- changes its onscreen appearance or reacts to user actions, for example -- must override the start() method. The start() method either performs the applet's work or (more likely) sets up a helper object to perform the work.

Most applets that override start() should also override the stop() method. The stop() method should suspend the applet's execution, so that it doesn't take up system resources when the user isn't viewing the applet's page. For example, an applet that displays animation should stop displaying the animation when the user isn't looking at it.

Many applets don't need to override the destroy() method, since their stop() method (which is called before destroy()) does everything necessary to shut down the applet's execution. However, destroy() is available for applets that need to release additional resources.

The init(), start(), stop(), and destroy() methods are discussed throughout this trail. For more information, you can also refer to the Applet API reference page.


Previous | Next | Trail Map | Writing Applets | Overview of Applets