Life Cycle of Java Applet

Last Updated : 5 Jun, 2026

A Java Applet is a small Java program that runs inside a web browser or an applet viewer. It follows a predefined life cycle managed by the applet container, which controls how the applet is loaded, executed, and terminated. Understanding the applet life cycle helps in managing resources and displaying content effectively.

  • Applets run on the client side and generate dynamic content.
  • They do not use the main() method for execution.
  • The applet container manages the complete applet life cycle.

Hierarchy of Java Applet

The Applet class is part of the AWT hierarchy and inherits functionality from several classes.

object

Hierarchy Explanation

  • Object – The root class of the Java class hierarchy. Every class in Java directly or indirectly inherits from Object, which provides common methods such as toString(), equals(), hashCode(), and clone().
  • Component – The superclass of all AWT graphical components. It provides basic functionality such as displaying objects on the screen, handling events, setting size and position, and managing colors and fonts.
  • Container – A subclass of Component that can hold and organize other GUI components. It provides methods to add, remove, and manage child components within a graphical interface.
  • Panel – A lightweight container used to group and arrange related components inside another container such as a Frame. It helps organize the user interface using layout managers.
  • Applet – A specialized Panel used to create small Java programs that run inside a web browser or applet viewer. It supports lifecycle methods such as init(), start(), stop(), and destroy(). (Applets are now deprecated and no longer supported in modern Java versions.)

Stages in the Life Cycle of Java Applet

The life cycle of an applet consists of five stages:

1. Initialization (init())

  • First method called when the applet is loaded.
  • Used to initialize variables, objects, and resources.
  • Executed only once during the applet's lifetime.

Syntax:

public void init() {
// Initialization code
}

There is no main method unlike our normal java programs. Every Applet will start it's execution from init() method. It is executed only once

2. Starting (start())

  • Called after init().
  • Starts or resumes the applet execution.
  • Invoked whenever the applet becomes active.

Syntax:

public void start() {
// Start applet code
}

3. Painting (paint(Graphics g))

  • Displays text, graphics, and shapes on the applet window.
  • Called whenever the applet needs to redraw its output.
  • Receives a Graphics object for drawing.

Syntax:

public void paint(Graphics g) {
// Drawing code
}

4. Stopping (stop())

  • Called when the user leaves the page or minimizes the browser.
  • Used to pause threads and background tasks.
  • The applet can be restarted later using start().

Syntax:

public void stop() {
// Stop applet code
}

5. Destroying (destroy())

  • Called before the applet is removed from memory.
  • Used to release resources and perform cleanup.
  • Executed only once.

Syntax:

public void destroy() {
// Cleanup code
}

Java Applet Implementation

Java Applets can be implemented and executed in two ways: using an HTML file or using the Applet Viewer tool. Both methods load and run the applet, but they differ in how the applet is executed.

1. Using an HTML File

In this approach, the applet is embedded inside an HTML page using the <applet> tag. When the HTML file is opened in a Java-enabled browser, the applet runs within the web page.

  • The applet is linked through an HTML file.
  • Uses the <applet> tag to specify the applet class, width, and height.

2. Using the Applet Viewer Tool

Applet Viewer is a utility provided by the JDK that allows applets to run without a web browser. It is mainly used for testing and development purposes.

  • No browser is required.
  • Faster and easier for testing applets.

Java Applet Life Cycle Methods

There are five methods of an Applet Life Cycle .

init_

All these are available in AWT Packagejava.awt.applet.* and in order ton import paint (Graphics g) we do use  java.awt.component package 

1. init()  

  • This is the first method to be called
  • Variables can be initialized here
  • This method can be called only once during the run time of the applet
  • It is invoked at the time of Initialization

Syntax of init(): 

public void init()
{
// To initialize objects
}

2. start()  

  • This method is called after init() method
  • start() method is used for starting the applet
  • It is also called to restart an applet after it has been stopped. i.e. to resume the applet

Syntax of start(): 

public void start()
{
// To start the applet code
}

Note: init() is called once i.e. when the first time an applet is loaded whereas start( ) is called each time an applet’s HTML document is displayed onscreen.

3. paint() 

void paint(Graphics g){ }

  • paint() method is used for painting any shapes like square, rectangle, trapeziums, etc.
  • paint() method has one parameter of type Graphics Class, this Graphics class enables the painting features in an applet.
  • This parameter will contain the graphics context, which is used whenever output for the applet is required.

Syntax of paint(): 

public void paint(Graphics graphics)
{
// Any shape's code
}

Note: This is the only method among all the method mention above, which is parameterized. 

4. stop() 

  • It is invoked every time the browser is stopped, minimized or when there is an abrupt failure in the application.
  • After stop()method called, we can also use start() method whenever we want.
  • This method mainly deals with clean up code.
  • The stop( ) method is called when a web browser leaves the HTML document containing the applet when it goes to another page, for example, when stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.

Syntax of stop(): 

public void stop()
{
// To stop the applet code
}

5. destroy() 

  • destroy() method is used to destroy the application once we are done with our applet work. It can be invoked only once.
  • Once applet is destroyed we can’t start() the applet (we cannot restore the applet again)
  • The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory.

Syntax of destroy(): 

public void destroy()
{
// To destroy the applet
}

Note: The stop( ) method is always called before destroy( )

Syntax: Entire Applet Life Cycle 

Java
Class AppletLifeCycle extends Applet
{
    public void init()
    {
        // Initializes objects
    }
    public void start()
    {
        // Starts the applet code
    }
    public void paint(Graphics graphics)
    {
        // Any shape's code
    }
    public void stop()
    {
        // Stops the applet code
    }
    public void destroy()
    {
        // Destroys the applet code
    }
}

 
Implementation: 

Example 1: In order to begin with Java Applet, let's understand a simple code to make the Applet  

Java
// Importing required classes from packages 
import java.awt.*;
import java.awt.applet.*;

// Class 1
// Helper class extending Applet class 
public class AppletDemo extends Applet

// Note: Every class used here is a derived class of applet,
// Hence we use extends keyword Every applet is public
{
    public void init()
    {
        setBackground(Color.black);
        setForeground(Color.yellow);
    }
    public void paint(Graphics g)
    {
        g.drawString("Welcome", 100, 100);
    }
}

// Save file as AppletDemo.java in local machine 
HTML
<html>
  <applet code = AppletDemo
          width = 400
          height = 500>
    </applet>
  </html>
<!-- Save as Applet.html -->

Compilation method

Method 1: Using the command

Compilation:

c:> javac.AppletDemo.java

Execution:

Double click on Applet.html

This won't work on browser as we don't have the proper plugins.

Method 2: Include the applet code in our java program make sure to put this html applet code as comments as it is important evil as demonstrated below as follows:

Example:

Java
// Java Program to Illustrate Insertion of HTML File in
// Applet As Commands

// Importing required classes
import java.applet.*;
import java.awt.*;

// Note: Insertion of HTM:L file as comments

/* <applet code = AppletDemo width=400 height=500>
</applet>*/

// Java Program

// Class extending Applet
public class AppletDemo extends Applet {
    public void init()
    {
        setBackground(Color.black);
        setForeground(Color.yellow);
    }
    public void paint(Graphics g)
    {
        g.drawString("Welcome to Applets", 50, 50);
    }
}

 
Compilation: 

c:\> javac AppletDemo.java

Execution: 

c:\> appletviewer AppletDemo.java

Comment