Last Updated on December 27, 2023 by Ankit Kochar
Java applets, once a popular way to enhance web pages with dynamic content, follow a specific life cycle that governs their initialization, execution, and termination. The applet life cycle is a sequence of methods that are called by the Java Virtual Machine (JVM) to manage the applet’s execution. Understanding this life cycle is crucial for Java developers creating interactive content for web applications.
The applet life cycle includes methods such as init(), start(), stop(), and destroy(), each serving a distinct purpose in the applet’s journey. From initialization to termination, these methods allow developers to handle resource allocation, start and stop animations or processes, and perform cleanup tasks.
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 conclusion, the applet life cycle in Java is a structured sequence of methods that govern the behavior of Java applets within a web page. The methods init(), start(), stop(), and destroy() play integral roles in managing the applet’s lifecycle, enabling developers to initialize resources, start and stop processes, and clean up resources appropriately.
While the use of Java applets has declined due to advancements in web technologies, understanding the applet life cycle remains valuable for historical context and for developers maintaining or updating existing applet-based applications.
Frequently Asked Questions(FAQs) related to Applet Life Cycle in Java
Here are the Frequently Asked Questions related to the Applet Life Cycle in Java.
1. What is the purpose of the init() method in the applet life cycle?
The init() method is called when an applet is initialized. It is used to perform one-time initialization tasks such as setting up graphical components or loading resources needed for the applet.
2. When is the start() method called in the applet life cycle?
The start() method is called after the init() method and is used to start or resume the execution of an applet. It is often used to initiate animations or other ongoing processes.
3. How does the stop() method function in the applet life cycle?
The stop() method is called when an applet is about to be stopped, either because the user navigates away from the web page or another event triggers the applet to stop. It is used to pause or stop ongoing processes.
4. What is the role of the destroy() method in the applet life cycle?
The destroy() method is called when an applet is about to be unloaded or destroyed. It is used to release resources, close files, and perform any necessary cleanup tasks before the applet is removed from memory.
5. Can the applet life cycle methods be overridden by the developer?
Yes, developers can override the applet life cycle methods to provide custom behavior. By overriding these methods, developers can tailor the applet’s behavior to meet specific requirements.
6. Are Java applets still widely used in web development?
No, Java applets have become less common in web development due to security concerns, lack of support on mobile devices, and the rise of alternative technologies such as HTML5, JavaScript, and WebAssembly. Modern web applications typically use these technologies for dynamic and interactive content.