Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Hierarchy of Applet in Java
3.
Methods of Applet Life Cycle
3.1.
init()
3.2.
start()
3.3.
paint (Graphics g)
3.4.
stop()
3.5.
destroy()
4.
Working of Applet Life Cycle
5.
States of Applet in Java  Life Cycle
5.1.
Born or Initialization State
5.2.
Running State
5.3.
Stopped or Idle State
5.4.
Dead State
6.
Syntax of entire Applet Life Cycle in Java
7.
Frequently Asked Questions
7.1.
What are the phases of applet lifecycle?
7.2.
What are the 5 methods of applet? 
7.3.
At what life cycle is the applet first loaded?
7.4.
What is the difference between init and start in applet?
8.
Conclusion
Last Updated: Apr 4, 2024
Medium

Applet Life Cycle in Java

Author Vikash Kumar
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

In the early days of the internet, web pages were largely static and lacked interactivity. However, with the introduction of Java Applets, web developers could add dynamic and interactive elements to web pages. 

Applet Life Cycle in Java

Java Applets were created as a way to run Java code within a web browser, allowing developers to create complex graphical user interfaces and animations that were not possible with HTML alone. The applet life cycle in Java is the process that an applet goes through, from the time it is loaded by the web browser to the time it is unloaded.

Hierarchy of Applet in Java

The hierarchy of Applet in Java can be depicted as follows:

  • Object: At the root of the Java class hierarchy is the Object class. Every class in Java is a direct or indirect subclass of Object. It provides basic functionalities that are common to all objects in Java, such as toString(), equals(), and hashCode().
  • Component: The Component class is an abstract base class for all AWT (Abstract Window Toolkit) components. It represents visual elements that can be added to a graphical user interface (GUI). Examples of components include buttons, text fields, labels, and checkboxes. Component provides methods for managing properties and events common to all GUI components.
  • Container: The Container class is a subclass of Component and represents a generic container for holding other components. Containers can include frames, panels, and applets. They provide a way to organize and manage the layout of GUI components within a window.
  • Panel: The Panel class is a lightweight container that extends Container. It is used to group and organize other components within a window. Panels can be added to frames, applets, or other containers to create complex layouts. They are commonly used for organizing GUI elements into sections or groups.
  • Applet: The Applet class is a subclass of Panel and represents the base class for creating applets in Java. Applets are small Java programs that run within a web browser. They provide interactive content and are typically used for creating animations, games, and multimedia applications on the web.
  • JApplet: The JApplet class is a subclass of Applet and is part of the Swing framework, which is Java's GUI toolkit. JApplet provides additional functionality and features compared to the basic Applet class. It allows developers to create applets with more sophisticated user interfaces using Swing components such as JButton, JLabel, JTextField, and others.
Hierarchy of Applet in Java
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Methods of Applet Life Cycle

The functions involved in the Applet life cycle in Java are:

init()

This method is called when the Applet is first loaded into memory. It initializes the Applet and sets its default values. This method is called only once in the Applet life cycle.

start()

This method is called after the init() method. It starts the execution of the Applet and performs all necessary tasks.

paint (Graphics g)

This method is called whenever the Applet needs to be painted on the screen. It is responsible for all the necessary drawing tasks.

stop()

This method is called when the Applet is stopped. It stops the execution of the Applet and releases any resources that it was using.

destroy()

This method is called when the Applet is being removed from memory. It releases all of the resources used by the Applet.

Working of Applet Life Cycle

The working Java Applet Life cycle is as follows:

  • The Applet life cycle is managed by the Java plug-in program
     
  • An applet is a Java application implemented in any web browser and works on the client side. It does not contain the main() method as it runs in the browser. It is therefore created to be placed on an HTML page
     
  • The awt.Component class consists of the paint() method
     
  • The applet.Applet class consist of start(), init(), destroy(), stop() methods
     
  • If we want to make a class an Applet class in Java, we are required to extend the Applet
     
  • Whenever an applet is created, we create an instance of the existing Applet class. And therefore, we can use all the methods of that class

States of Applet in Java  Life Cycle

Let us now check out the states the applet goes through in its life cycle:

Born or Initialization State

The first state of the Applet life cycle is initialization. This is done by calling the () method. This state is invoked only once when the Applet is first loaded into memory. During this state, the Applet initializes itself and sets its default values. At this stage we can do the following actions-creating objects, initializing values, loading colors or images, and setting up the colors.  

Running State

The running state is the state where the Applet is actively running and performing its tasks. It is in this state that the user interacts with the Applet. If we leave the web page containing the Applet temporarily to another page, it pushes the Applet to an idle state. When we return back to the page, this again starts the Applet running. Unlike the init() method, the start() method can be invoked more than once. 

Stopped or Idle State

An Applet becomes idle when it is stopped. This can be invoked by calling the stop() method. In this state, the Applet stops running, and it releases any resources that it was using. Stopping occurs automatically when we leave the current web page on which the Applet was running. We can also do this by calling the stop() method.

Dead State

The dead state is invoked when the Applet is removed from memory. In this state, the Applet releases all of its resources, such as file handles, network sockets, and database connections. This occurs automatically by invoking the destroy() method. This happens when we quit the browser in which it was running. Like initialization, destroying also occurs only once.
 

Note- Display: Whenever an Applet has to perform some output operation, it enters the display state. This state can be invoked only after the Applet enters the running state. paint() method is called to accomplish this task.

applet life cycle

Syntax of entire Applet Life Cycle in Java

Below is the syntax of the code that shows the applet life cycle in Java:

import java.applet.Applet;
import java.awt.Graphics;


public class MyApplet extends Applet {
    
    // Initialization state
    public void init() {
        // This method is called only once, when the Applet is first loaded into memory.
        // Initialize the Applet and set its default values.
    }
    
    
    public void start() {
        // This method is called after the init() method.
        // Start the execution of the Applet and perform all necessary tasks.
    }
    
    // Running state
    public void paint(Graphics g) {
        // This method is called whenever the Applet needs to be painted on the screen.
        // Perform all necessary drawing tasks here.
    }
    
    // Idle state
    public void stop() {
        // This method is called when the Applet is stopped.
        // Stop the execution of the Applet and release any resources that it was using.
    }
    
    // Dead state
    public void destroy() {
        // This method is called when the Applet is being removed from memory.
        // Release all of the resources used by the Applet.
    }
}

Frequently Asked Questions

What are the phases of applet lifecycle?

The phases of the applet lifecycle are the initialization, start, stop, and destruction phases, which are used for initialization, to start, stop, and for destruction, respectively. These phases tell about the basic lifecycle of applet.

What are the 5 methods of applet? 

Applet lifecycle consists of five method, init() is used for initialization, start() is used to start, stop() is used to stop the applet application, paint() is used to paint shapes, and destroy is used to destroy the applet.

At what life cycle is the applet first loaded?

Initialization is the first stage of the Applet life cycle. The init() method is called to accomplish this. This state is only called once. The Applet initializes itself in this phase and sets its default values.

What is the difference between init and start in applet?

In an applet, the init() method is called once to initialize the applet, typically used for setting up resources. The start() method is invoked after init(), signaling the applet to begin execution or resume after being paused.

Conclusion

Hope this article helped you to have a better understanding of the applet life cycle in Java, the different stages involved in the creation of an applet, and how they interact with each other. As with any programming language, learning Java takes time and practice. You can head out to our platform code studio and learn more. Code studio provides guided paths for topics you want to learn. This can help you to learn new things quickly and efficiently.

Check out other related articles to learn more: 

Happy Coding!

Previous article
Difference Between Callable and Runnable in Java
Next article
Difference Between a Java Application and a Java Applet
Live masterclass