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 this article we will learn about applet life cycle and various life cycle methods of an applet along with example program.

 

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

 

applet-life-cycle

 

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.

 

Below is the description of each applet life cycle method:

 

init(): The init() method is the first method to execute when the applet is executed. Variable declaration and initialization operations are performed in this method.

 

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.

 

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.

 

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. The destroy() method removes the applet object from memory.

 

paint(): 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.

 

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 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

 

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.

OH WELL SIR U ARE GREAT SIR

sir, you made it the easiest way to understand the life cycle of applet.
thanks a lot…
keep explaning…

sir, you made it the easiest way to understand the life cycle of applet.
thanks a lot…

Awesome

Thanku so much sir ..
Very exllent method…

i dnt get the difference between start() and paint() method, i mean both runs the code within applet. how? please reply?

    Hi amit,

    The sequence in which the methods execute when an applet is started is init, start, and paint. The paint method can be used to display graphics or form controls like textbox, buttons etc., which cannot be done using start method.

    So we use start method to write code that executes some initialization code every time the applet is maximized and paint method to write code for displaying controls and graphics.

It should be easy , thank you sir
But we want more information about this topic

Tqqq this is helpful

awesome

It is easy you dumb, you want much easier?? Go study alphabets

Leave a Reply

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