How do Applet Life Cycle Methods Work in Java?

shivam bhatele
JavaToDev
7 min readMar 31, 2023

--

Java Applet

In java, for dynamic content generation, a special type of program is made to be embedded with the web page and this program is termed an applet. We can also define an applet as a class in java. The life cycle of the applet is the process in which an object of Java is created, initiated, ended, and destroyed in its whole execution cycle in an application. There are five main methods in the applet life cycle. These are: init(), start(), stop(), paint() and destroy(). These methods are invoked for execution.

  • An applet is also a class Java which is created by extending the class java.applet.Applet.
  • the main() method is not defined inside an applet means in an applet there is no invocation of the main().
  • Applets are created to be embedded with an HTML page
  • The applet code is downloaded to the local machine of the user when the web page containing an applet is viewed by the user.
  • For execution and for viewing an applet, JVM is required. The JVM creates object instances on the local machine of the user and then the JVM invokes the various methods during an applet life cycle.

What are the Types of Applets in Java?

Two types of the applet are available in java. These are:

  1. Local applet
  2. Remote applet

Local Applet

Local applets are allowed to be written on their own and then embedded into the web pages. It is stored in the local system or machine as it is developed on a local level. The local applet does not allow the web page to access data through the internet. In place of that, it is only specified by the local pathname or filename. Two specific attributes are used for describing the applet in java. The first attribute contains the code that represents the name of the path and the second attribute represents the code that has the filename that contains the code of the applet.

Remote Applet

The remote applet is not developed by the java team. Instead of that, remote applets are designed and developed by another developer. It is located on a computer that is remotely available and must have a connection to the internet. The system must have an internet connection for downloading and running the applet program stored in the remote system. The loading of the remote Applet requires the URL (Uniform Resource Locator) i.e applet address on the web page.

How Do Applet Life-Cycle Methods Work in Java?

All the management of the life cycle of the applet is done by the plugin software present in java. Its execution can be done in any browser. And the client side of an application requires it to work. There is no involvement of any main() method as it executes on the browser. So, it is mainly designed for HTML pages.

The start(), init(), stop(), and destroy() methods of the apple life cycle belong to the applet.Applet class and paint() method of the applet life cycle belongs to the awt.Component class.

The applet class is required to be extended for the creation of the normal class to be an applet class.

When there is a creation of any applet class, then its instances are created automatically and permit us to access all the available methods of the class. The applet can be automatically invoked by the browser and there is no requirement to call methods explicitly, also there is no need of declaring the constructors multiple times to make it overload.

Method 1: init() method:

  • init() is the first method called in the life cycle
  • Initialization of all the variables is done here.
  • During run time, init() is called once only.
  • Its invocation is done at the initialization time.

Syntax of init() method of applet life cycle

public void init()
{
// for objects initialization
}

Method 2: start() method

  • start() method is called after calling the init() method
  • This method is called for starting an applet.
  • If the applet is stopped, then also this method is called for the applet restarting

Syntax of start() method of applet life cycle

public void start()
{
// Code for starting the applet
}

Method 3: paint() method

  • To paint different shapes such as rectangles, squares, etc, we use this method.
  • The painting feature is enabled in the applet by the graphics class type passed as an argument.
  • The graphics context is included in the graphics class argument which displays the applet output

Syntax of paint() method of applet life cycle

public void paint(Graphics graphics)
{
// Code of any shape creation
}

Method 4: stop() method

  • When the browser stops working, minimizes or any abrupt failure is found in the application, then this method is invoked.
  • Mostly, the start method can be implemented after the stop method.

Syntax of stop() method of applet life cycle

public void stop()
{
// Code to stop an applet
}

Method 5: destroy() method

  • This method is invoked once only and invoked to destroy an application when we are done with the work of an applet.
  • Applets can not be restored after destroying them.
  • When the environment finds that the applet is required to be removed completely from the memory then this method is called.

Syntax of destroy() method of applet life cycle

public void destroy()
{
// code for destroying the applet
}

Example1 :

Below is a simple code implementation of an applet in java for a better understanding of an applet

// importing packages
import java.awt.applet.*;
import java.awt.*;
// creating a class AppletExample by extending an Applet class
public class AppletExample extends Applet {
// init() method of an applet life cycle
public void init() {
// setting the foreground color of an applet
setForeground(Color.green);
// setting the background color of an applet
setBackground(Color.red);
}
// paint() method of an applet life cycle
public void paint(Graphics g) {
// drawing a string “Hello!! Welcome to our applet” on the screen
g.drawString("Hello!!Welcome to our applet", 100, 100);
}
}

The file in which the above code is written is save that file in the local machine with the name AppletExample.java

<html>
<applet code = AppletExample width = 500 height = 600>
</applet>
</html>

The above written code in the file and that file must be saved as AppletHtmlPage.html

Type the below-given command for program execution and compilation

javac AppletExample.java appletviewer AppletExample.java

Explanation: As soon as the program execution starts, a window with the red background color pops up on the screen and on that screen a string “Hello!! Welcome to our applet” with the green color displayed.

Example 2

Below is a code implementation for a better understanding of the life cycle methods of a java applet.

// importing packages
import java.awt.applet.*;
import java.awt.*;
// code needs to be written for this file in the HTML file
/*<applet code="AppletDemoLifeCycleMethod.class" width="350" height="150"> </applet>*/
// creating a class AppletDem0LifeCycleMethod by extending an Applet class




public class AppletDem0LifeCycleMethod extends Applet {
// init() method of an applet life cycle
public void init() {
// setting the background color to red
setBackground(Color.RED);
System.out.println("init() method of an applet life cycle ");
}
// start() method of an applet life cycle




public void start() {
System.out.println("start() method of an applet life cycle is invoked");
}
// paint() method of an applet life cycle
public void paint(Graphics g) {
System.out.println("paint() method of an applet life cycle is invoked");
}
// stop() method of an applet life cycle
public void stop() {
System.out.println("stop() method of an applet life cycle is invoked");
}
// destroy() method of an applet life cycle
public void destroy() {
System.out.println("destroy() method of an applet life cycle is invoked");
}
}

Explanation: As soon as program execution starts, a pop window appears and every applet class is created. Due to this, the init() method of the java applet life cycle is invoked and performs the work defined inside the init() method. It set the background color of an applet to red and displays the message “init() method of an applet life cycle is invoked”. After that, the start() method is invoked and displays a message “start() method of an applet life cycle is invoked”.

After that “paint() method of an applet life cycle is invoked” is displayed due to the invocation of paint() method of an applet life cycle. The stop() method is invoked when the user clicks the minimize button and then the start() method is invoked for window restarting after minimizing it and at the time of closing of the window, for window closing destroy() method of an applet life cycle is invoked.

Conclusion

In conclusion, understanding the Applet life cycle methods in Java is crucial for developing Java applets. These methods provide a framework for the applet to be initialized, started, stopped, and destroyed. By implementing these methods correctly, developers can ensure that their applets are properly initialized and executed, leading to a better user experience. Furthermore, knowing how to properly use these methods can help prevent memory leaks and improve the overall performance of the applet. It is essential for developers to have a strong understanding of the Applet life cycle methods to create functional and efficient Java applets.

--

--

shivam bhatele
JavaToDev

I am a Software Developer and I loved to share programming knowledge and interact with new people. Also I am big lover of dogs, reading, and dancing.