Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Applets. 23 Comments on Applet life cycle in java with example program
3.9
(15)

In this article we will learn about applet life cycle and various life cycle methods of an applet along with example program.

 

Introduction to life cycle of an applet

What is applet life cycle?

The applet life cycle refers to the stages an applet goes through from its creation to its termination when run in a web browser.

 

It starts with the init() method, which initializes the applet, followed by the start() method, which starts the applet’s execution. When the user navigates away from the applet’s page, the stop() method pauses the applet. If the applet is no longer needed, the destroy() method is called to clean up resources.

 

Additionally, the paint(Graphics g) method is used to display the applet’s output on the screen. These stages ensure that the applet initializes correctly, runs smoothly, and shuts down properly.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Importance of the life cycle in applet development

The life cycle in applet development is important because it ensures that the applet initializes, runs, pauses, and shuts down properly. By following the life cycle methods (init(), start(), stop(), and destroy()), developers can manage resources efficiently, handle user interactions smoothly, and avoid issues like memory leaks or crashes. Understanding the life cycle helps create stable and responsive applets that work well within a web browser.

 

Applet life cycle diagram

The life cycle of an applet is as shown in the figure below:

Applet life cycle in java

 

As shown in the above diagram, the life cycle of an applet starts with init() method and ends with destroy() method. Other life cycle methods are start(), stop() and paint().

 

The methods to execute only once in the applet life cycle are init() and destroy(). Other methods execute multiple times.

 

Key methods in life cycle of an applet

The four applet life cycle methods are: init(), start(), stop() and destroy().

 

init(): The init() method is the first method to execute when the applet is executed. It starts the execution of an applet. Variable declaration and initialization operations are performed in this method. Syntax of init() method is shown below:

public void init()
{
	// Object initialization code
}

 

start(): 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. Syntax of start() method is given below:

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

 

stop(): 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. Syntax of stop() method is shown below:

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

 

destroy(): The destroy() method executes when the applet window is closed or when the tab containing the webpage is closed. stop() method executes just before when destroy() method is invoked. This removes the applet object from memory. Syntax of destroy() method is shown below:

public void destroy()
{
	// Code to execute when applet terminates
}

 

paint(): The paint() method is used to redraw the output on the applet display area. This method executes after the execution of start() method and whenever the applet or browser is resized. The paint() method by far acts as main method in an applet. Syntax of paint() method is shown below:

public void paint(Graphics graphics)
{
	// graphics related code
}

 

The method execution sequence when an applet is executed is:

  • init()
  • start()
  • paint()

 

The method execution sequence when an applet is closed is:

  • stop()
  • destroy()

 

Example program for applet life cycle

Example code that demonstrates the life cycle of an applet is as follows:

import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet 
{
	public void init()
	{
		System.out.println("Applet initialized");
	}
	public void start()
	{
		System.out.println("Applet execution started");
	}
	public void stop()
	{
		System.out.println("Applet execution stopped");
	}
	public void paint(Graphics g)
	{
		System.out.println("Painting...");
	}
	public void destroy()
	{
		System.out.println("Applet destroyed");
	}
}

 

Output of the above applet program when run using appletviewer tool is:

Applet initialized
Applet execution started
Painting…
Painting…
Applet execution stopped
Applet destroyed

 

Program explanation

The above java program defines an applet named MyApplet that extends the Applet class. Here’s a breakdown of what each method does:

init() method

  • This method is called when the applet is initialized.
  • In this program, it prints “Applet initialized” to the console.
  • It is typically used for one-time initialization tasks such as setting up variables or loading resources.

start() method

  • This method is called when the applet execution starts or resumes.
  • It prints “Applet execution started” to the console.
  • It is often used to start activities or threads that need to run when the applet becomes active.

stop method

  • This method is called when the applet execution is paused or stopped.
  • It prints “Applet execution stopped” to the console.
  • It is used to suspend or clean up activities that are no longer needed when the applet is not visible or active.

paint method

  • This method is called automatically whenever the applet needs to redraw its graphical output.
    It prints “Painting…” to the console in this program.
    It is where you typically draw graphics or update visual elements of the applet using the Graphics object g.

destroy() method

  • This method is called when the applet is being destroyed or unloaded from the system.
  • It prints “Applet destroyed” to the console.
  • It is used to release resources, close files, or perform any final cleanup before the applet is completely removed from memory.

Execution flow

When the applet is loaded in a web browser or applet viewer:

  1. The init() method is called first, initializing the applet and printing “Applet initialized”.
  2. Next, the start() method is called, starting the execution of the applet and printing “Applet execution started”.
  3. During the execution, the paint(Graphics g) method might be called multiple times whenever the applet needs to repaint itself, printing “Painting…”.
  4. If the applet is minimized or another window overlaps it, the stop() method might be called, printing “Applet execution stopped”.
  5. Finally, when the applet is closed or the web page is navigated away from, the destroy() method is called, printing “Applet destroyed”.

 

Further reading and resources

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

23 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

Thanks mate, it was very helpful and simple to learn.❤️

It’s very easy to understand,tq sir

    🙂

Tq sir it is really easy to write and understand

    🙂

Concept is clear.. Easy to write…
Thnqq soo mch

It should be easy not esay

you have made applet life cycle very esay.
Thanks sir

awesome

Thanks sir in question is answer thank you so much

ur concept is good sir

ur super guru ji

Leave a Reply

Your email address will not be published. Required fields are marked *