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


The Life Cycle of an Applet

Below is the Simple applet.


You can't run applets. Here's what you'd see if you could:


Loading the Applet

You should see "initializing... starting..." above, as the result of the applet being loaded. When an applet is loaded, here's what happens:

Leaving and Returning to the Applet's Page

When the user leaves the page -- for example, to go to another page -- the applet has the option of stopping itself. When the user returns to the page, the applet can start itself again. The same sequence occurs when the user iconifies and then reopens the window that contains the applet.

Try this: Leave and then return to this page. You'll see "stopping..." added to the applet output above, as the applet is given the chance to stop itself. You'll also see "starting...", when the applet is told to start itself again.

Browser note: If you're running the Netscape Navigator 2.0 or 2.01 browser, you might notice that the applet initializes itself more than once. Specifically, the browser asks the applet to initialize itself whenever the user returns to the applet's page by following a link. The browser does not ask the applet to initialize itself when the user returns to the page using the Back button. The latter is the expected behavior.

Try this: Iconify this window, and then open it again. Many window systems provide a button in the title bar that lets you iconify the window. (Other terms used instead of iconify are miniaturize, minimize, and close.) You should see "stopping..." and then "starting..." added to the applet output above.

Reloading the Applet

Some browsers let the user reload applets, which consists of unloading the applet and then loading it again. Before an applet is unloaded, it's given the chance to stop itself and then to perform a final cleanup, so that the applet can release any resources it holds. After that, the applet is unloaded and then loaded again, as described in Loading the Applet, above.

Try this: If your browser or other applet viewer lets you easily reload applets, reload the applet. Look at the standard output to see what happens when you reload the applet. You should see "stopping..." and "preparing for unloading..." when the applet is unloaded. (You can't see this in the applet above because the applet is unloaded before the text can be displayed.) When the applet is reloaded, you should see "initializing..." and "starting...", just like when you loaded the applet for the first time.

Browser Note: The Netscape Navigator 2.0 browser will sometimes reload the applet if you Shift-click the reload button. If that fails, you can try emptying Netscape's memory and disk caches (from the Options/Network Preferences dialog) and reloading again.

Quitting the Browser

When the user quits the browser (or whatever application is displaying the applet), the applet has the chance to stop itself and do final cleanup before the browser exits.

Summary

An applet can react to major events in the following ways: The next page describes the four applet methods that correspond to these four types of reactions.


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