by Dinesh Thakur

When an applet is executed within the web browser or in an applet window, it goes through the four stages of its life cycle: initialized, started, stopped and destroyed. These stages correspond to the applet methods init (), start (), stop () and destroy () respectively. All these methods are defined in the Applet class which are called automatically by the browser or the applet viewer controlling the applet. All these methods have empty bodies by default. To perform specific functions, these methods need to be overridden in the user's applet so that the browser can call your code properly.

                            Applet Life Cycle

We shall now briefly discuss about these methods:

Public void init (): This method is used to perform any initialization that is needed for the applet. It is called automatically when the applet is first loaded into the browser and is called only once during the life cycle of the applet.

In this method, we generally perform startup activities such as adding GUI components; loading resources such as images, audio, font; creating threads; and getting string parameter values from the APPLET tag in the HTML page.

Public void start (): After the applet is loaded and initialized, the Java environment automatically calls the start () method. It is also called when the user returns to the HTML page that contains the applet after browsing through other webpages. This method is used to start the processing for the applet. For example: Action performed here might include starting an animation or starting other threads of execution. Unlike init () method, the start () method may be called more than once during the life cycle of an applet.

Public void stop (): This method is called automatically by the browser when the user moves off the HTML page containing the applet. It is generally used to suspend the applet's execution so that it does not take up system resources when the user is not viewing the HTML page or has quit the browser. For example : Processor intensive activities such as animation, playing audio files or performing calculations in a thread can be stopped which will not be visible to the user until the user returns to the page.

Public void destroy (): This method is called after the stop () method when the user exits the web browser normally. This method is used to clean up resources allocated to the applet that are managed by the local operating system. Like init () method, it is called only once in the lifetime of the applet.

It is not always necessary to override all of these methods to get a basic applet to work. You will most likely need to implement only the init () method at minimum. The need to implement the remaining life cycle methods depends on what your applet is doing.

import java.applet.Applet;

import java.awt.Graphics;
import java.awt.*;
/*<applet code="AppletLifeCycle.class" width="350" height="150"> </applet>*/
public class AppletLifeCycle extends Applet
     public void init()
       System.out.println("init() called");
     public void start(){ System.out.println("Start() called"); }
     public void paint(Graphics g){ System.out.println("Paint(() called"); }
     public void stop() { System.out.println("Stop() Called"); }
     public void destroy()   { System.out.println("Destroy)() Called"); }

Applet Life Cycle in Java with Example