by Dinesh Thakur Category: AWT and Applets

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 defined in the Applet class which is called automatically by the browser or the applet viewer controlling the applet. All these methods have hollow 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 correctly.

We shall now briefly discuss about these methods:

Public void init(): This method is used to perform any initialization that 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 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 the 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 not visible to the user until the user returns to the page.

Public void destroy(): This method 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 is managed by the local operating system. Like the 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 a minimum. The need to implement the remaining life cycle methods depends on what your applet is doing.

Applet Life Cycle

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

When the user opens Applet Life, an HTML page containing the applet life cycle. java in an app1etviewer, an instance for each of the applet classes is created using the no-argument constructor, and its init() method called. As a result, the message" init() called" is displayed in the command window. After execution of the code in the init() method, the program control returns to the applet viewer which then calls the applet's start () method. As a result, the message "start() called" is displayed. Next, the appletviewer calls the applet's paint() method as a result of which the message "paint() called" displayed.

Now when the user minimizes the applet window, the stop() method is called, and on restoring the window the start() method is called again. When the user exits the appletviewer, the destroy() method called that displays the message "destroy() called" in the command window. 

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles