Table of Contents

Java Applet Life Cycle

Every Java applet inherits a set of default behaviors from the Applet class. As a result, when an applet is loaded, it undergoes a series of changes in its state.

The applet state includes:

  1. Born on initialization state
  2. Running state
  3. Idle state
  4. Dead or destroyed state

1. Initialization State:

Applet enters the initialization state when it is first loaded. This is achieved by calling the init() method of the Applet class. The initialization occurs only once in the applet's life cycle.

public void init(){
.........
.........   (action)
}

2. Running State:

The start() method contains the actual code of the applet that should run. The start() method executes immediately after the init() method. It also executes whenever the applet is restored, maximized or moving from one tab to another tab in the browser.

public void start(){
.........
.........   (action)
}

3. Idle or Stopped State:

The stop() method stops the execution of the applet. The stop() method executes when the applet is minimized or when moving from one tab to another in the browser.

public void stop(){
.........
.........   (action)
}

4. Dead State:

The destroy() method executes when the applet window is closed or when the tab containing the web page is closed. The stop() method executes just before when destroy() method is invoked. The destroy() method removes the applet object from memory.

public void destroy(){
.........
.........   (action)
}

5. Display State:

The paint() method is used to redraw the output on the applet display area. The paint() method executes after the execution of start() method and whenever the applet or browser is resized.

public void paint(Graphics g){
.........
.........   (Display statements)
}

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?