Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Applet Life Cycle in Java

Last Updated on March 22, 2023 by Prepbytes

Applets in Java is small applications that run within a web browser and are used to create interactive graphics, animations, and User Interface(UI) and provide users with a better User Experience. Today, we will learn about the Applet Life Cycle in Java. Before delving deep into the topic, let us first learn about the Applets in Java and their types.

What is an Applet in Java?

An applet is a Java program that runs within a web browser. It is executed on the client side, which means it is downloaded from a web server to the client’s computer and then executed within the browser. Applets are designed to be portable and can be run on any platform that has a Java Virtual Machine (JVM) installed.

Hierarchy of Applet in Java

Applets is used by Java Developers to make websites more dynamic and interesting. The below image shows the hierarchy of Applets in Java.

Here are some important properties of Applets in Java:

  • Applets are executed in a web browser or an Applet Viewer,
  • Applets are derived from the “java.applet” class.
  • Applets in Java are not stand-alone programs.
  • It is not initiated in the main class.
  • We cannot use “System.out.println()” for displaying output on an applet.

Type of Applet in Java

In Java, applets are of two types:

  • Local Applet: It is written separately and then embedded into websites. It is created and stored locally on the system/machine. A web page in the local applet does not receive information from the internet; instead, it is specified by the local pathname or filename.
  • Remote Applet: It is created and designed by another developer, not the Java team. It is hosted on a remote computer that is connected to the internet. The system must be connected to the internet to download and run the applet application that is stored on a remote computer. The remote applet can only be loaded if the applet’s web address (Uniform Resource Locator) is known.

Applet Life Cycle in Java

The Applet Life Cycle in Java can be defined as the process of how an applet object is created, started, stopped, and destroyed during the entire execution of the applet.

There are mainly five core methods used in the Applet Life Cycle in Java namely,

  • init()
  • start()
  • paint()
  • stop()
  • destroy()

Here, the method paint() is defined in the “java.awt.component” class, while the other four methods belong to the “java.awt.applet”.

Since applets run on the browser, there is no main() method involved in the execution of the applet.

For creating an applet, we need to extend the Applet class to a normal class. When we create an Applet Class is created, it allows us to use all of the methods present in the class. These are invoked automatically by the browser, so need of calling the methods explicitly.

Let us learn about each stage of the Applet Life Cycle in Java along with the methods involved in the particular step.

  • Stage 1 of Applet Life Cycle in Java: Initialize the Applet
    Initialization is the first stage in Applet Life Cycle in Java. init() method is used for the initialization of the Applet since no main() method is used. This init() method is called only once for creating the applet. All the variables are initialized in this method.

    Syntax of init() method:

    public void init(){
          // To initialize objects
    }
  • Stage 2 of Applet Life Cycle in Java: Start the Applet
    The next step is starting the Java Applet. The start() method is used for starting the Applet in Java. This method is called after the init() method. This method contains the actual code of the applet.

    Syntax of start() method:

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

    The start() method is invoked every time the browser is loaded or refreshed. It is also called in the case the applet is maximized, restored, or moved from one tab to another.

  • Stage 3 of Applet Life Cycle in Java: Paint the Applet
    This step involves drawing various shapes in the applet using the paint() method. This method belongs to the Graphic Class. It consists of the parameter of class Graphics, which helps in enabling the painting in an applet.

    Syntax of paint() method:

    public void paint(Graphics graphics) {
            // Code for drawing shape
    }

    The paint() method is used to draw different shapes like rectangles and squares, etc.

  • Stage 4 of Applet Life Cycle in Java: Stop the Applet
    To stop an applet, we use the stop() method of the Applet class. This stop() method is invoked when the browser is minimized, restored, or moved to another tab. When we move to the page again, the start() method is called. This method is used for cleaning the code when the browser leaves the HTML document when the Java Applet is running.

    Syntax of stop() Method:

    public void stop(){
          // To stop the applet code
    }
  • Stage 5 of Applet Life Cycle in Java: Destroy the Applet
    We use the destroy() method for the purpose of destroying the applet. This is the last stage of the Applet Life Cycle in Java. This method is called when the applet is closed or the tab containing the webpage is closed. destroy() method can only be called once. If we destroy an applet, it cannot be restored.

    Syntax of destroy() method:

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

    destroy() method removes the applet object from the memory completely.

    As an applet starts working, the following methods are called in order:

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

    As an applet’s operation is about to come to an end, the following methods are called in order:

    • stop()
    • destroy()

Syntax of the Entire Applet Life Cycle in Java

Here is the syntax of Entire Applet Life Cycle in Java.

class MyApplet extends Applet {  
  public void init() {  
    // To initialize objects
  }  

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

  public void paint(Graphics graphics) {  
    // Code for drawing shape
  }  

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

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

Example of Applet Life Cycle in Java

Here is the example for a deeper understanding of the concept of Applet Life Cycle in Java.

Code for demonstrating the Applet Life Cycle in Java

import java.applet.*;
import java.awt.*;


public class MyApplet extends Applet {


  public void init() {
      // Initialization code here
  }


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


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


  public void destroy() {
      // Clean up code here
  }


  public void paint(Graphics g) {
      g.drawString("Hello, world!", 50, 25);
  }
}

Explanation of the above code:
In this applet, we have implemented all five methods of the applet life cycle.

  • The “init()” method is empty because we don’t need to initialize anything for this applet.
  • The “start()” method is also empty because we don’t have any animation or continuous processes in this applet.
  • The “stop()” method is also empty because we don’t have anything to stop.
  • The “destroy()” method is also empty because we don’t have any resources to release.
  • The “paint()” method is the only method that has code. It is called every time the applet needs to be redrawn on the screen. In this method, we have used the Graphics class to draw a string on the screen.

When this applet is loaded into a web browser, the following sequence of events occurs

  • Step 1: The browser calls the init() method to initialize the applet.
  • Step 2: The browser calls the start() method to start the applet.
  • Step 3: The browser calls the paint() method to draw the applet on the screen. The user sees the message "Hello, world!" on the screen.
  • Step 4: If the user navigates away from the page that contains the applet, or if the browser is closed, the browser calls the stop() method to stop the applet.
  • Step 5: If the applet is no longer needed, the browser calls the destroy() method to release any resources that were allocated by the applet.

Conclusion
In this article, we discussed the life cycle of applets in Java. We learned that the life cycle consists of four methods that are called in a specific order: init(), start(), paint(), stop(), and destroy(). We also saw simple examples of an applet that displays a message on the screen. Understanding the applet life cycle is important for developing applets that are efficient and bug-free. By properly implementing the five methods of the applet life cycle, developers can ensure that their applets work correctly and are released from memory when they are no longer needed.

Frequently Asked Questions(FAQs)

Here are the Frequently Asked Questions related to Applet Life Cycle in Java.

Ques 1. Can an applet be started and stopped multiple times during its life cycle in Java?
Ans. Yes, an applet can be started and stopped multiple times during its life cycle in Java.

Ques 2. What happens if you call the start() method before the init() method in Java applets?
Ans. If you call the start() method before the init() method in Java applets, the applet will throw an “IllegalStateException”.

Ques 3. What is the difference between the stop() and destroy() methods in Java applets?
Ans. The stop() method is used to pause the Java applet’s execution, while the destroy() method is used to clean up any resources that the applet has used.

Ques 4. Can you use applets in modern web browsers?
Ans. No, applets are no longer supported in modern web browsers because the security concerns. Instead of Java Applets, web developers typically use alternative technologies such as HTML5, CSS3, and JavaScript to create dynamic web content.

Leave a Reply

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