Java applets are not just small programs; they are versatile tools designed to support the dynamic content of web pages launched within a browser environment. They enable one to develop mechanisms that directly impact the user through the browser. These applets can be added to HTML pages to make the web experience much richer and even create interactive ones. Java application applets differ from other Java applications in that applets are meant to run as an application within a browser. Their versatility is truly inspiring for web developers and programmers.
Applets improve web content because they offer the means to create animations, games, and interactive applications. Adding applets to the web page provides the capability to add functionality that, if implemented using scripts, would be heavier or require one or more other plugins. However, nowadays, applets are less frequently used, and there are security issues as well as there are technologies that are more advanced than applet technologies, but knowing about the applet life cycle in Java is still useful in case you have to maintain an application that uses applets or it is useful to know for historical purposes.
Detailed Explanation of Applet Life Cycle in Java
Java applets adhere to a specific life cycle, a series of stages that govern their creation, execution, and termination. These stages are managed automatically by the browser, which calls specific methods. Grasping the functionality of these methods is not only crucial but also empowers those working with applets, equipping them with the necessary knowledge to effectively manage and troubleshoot applets.
Java applets have a definite life cycle, which is more like a set of stages that are used to create, run, execute, and even stop the applets. It is important for anyone who will be handling an applet to understand these methods. The browser invokes methods to control these stages, as described in the following steps.
Initialisation Stage: init() Method
The init() method is the first part of the applet life cycle in Java. It creates the applet and, if necessary, any local variables or other resources it needs. It is only called once when the applet is loaded as a startup routine to initialise its environment.
For example:
public void init() {
// Initialization code here
}
This method prepares the applet for execution, ensuring that all necessary components are ready.
Starting the Applet: start() Method
After the applet is created, the start method executes after it has been initialised. This method has the real code responsibility of making the applet work. When the HTML page of the applet is opened or reloaded, the start() method is invoked each time. It is also used when we minimise the browser window containing the applet and then restore it to the active window.
For instance:
public void start() {
// Start or resume the applet
}
Placing all the major functionality of the applet in the start () method means that it will run without any problems whenever it is required.
Painting the Applet: paint() Method
The paint() method handles the applet’s visual representation. It is called every time the applet needs to redraw its output. This method takes a Graphics object as a parameter, which provides the context for drawing.
The paint() method controls how the applet looks on the screen. It is called each time the applet refreshes its output on the screen or window. This method has a Graphics object as a parameter, which is used as the context for the next step.
Consider this example:
public void paint(Graphics g) {
g.drawString("Hello, Applet!", 20, 20);
}
Using the paint() method, we can draw shapes, text, and images, making the applet interactive and visually appealing.
Stopping the Applet: stop() Method
When the applet is no longer visible, such as when the user navigates away from the page, the stop() method is called. This method suspends any ongoing activities within the applet. If the user returns to the applet, the start() method is called again to resume its execution.
For example:
public void stop() {
// Pause the applet
}
The stop() method ensures that the applet does not consume resources when it is not active.
Destroying the Applet: destroy() Method
The destroy() method is the final stage of the applet life cycle. This method is called when the applet is about to be removed from memory. It allows us to clean up resources and perform any necessary finalisation tasks. The applet cannot be restarted once the destroy() method is called.
Here’s how it looks:
public void destroy() {
// Cleanup code here
}
This method ensures that all resources are released properly, preventing memory leaks and other issues.
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
The Role of the Browser and JVM in Applet Execution
The browser and JVM are involved in dealing with various phases of the applet life cycle in Java. As soon as we open a webpage that has an applet in it, the browser invokes JVM, which in turn makes an applet instance. The browser automatically invokes the life cycle methods init(), start(), paint(), stop(), and destroy() at the right time.
This makes developing and running applets much easier than when they have to be programmed manually. Thankfully, we do not need to call these methods by ourselves; the browser does this part for you. The JVM provides security features that prevent the applet from accessing other resources in the client machine; thus, the applet is contained in the running environment.
For instance, the init() method is invoked when the applet is started to set its initial state. When the user moves out, the stop() method halts the applet, while the destroy() method clears up when the browser is closed.
With this kind of management, we can be free of the inside and out details of its running and instead direct our attention towards what our applet is designed to accomplish. That is why applets are considered a very effective tool for including dynamic content into Web pages. However, with the modern threat of security breaches, new technologies have appeared in use. It is for this reason that the role of the browser, particularly the JVM, enables one to understand how applets can fit perfectly into web applications.
Practical Examples of Java Applets
Let’s examine some practical examples of Java applets to better understand them.
Code Example 1:
// Java program to run the applet
// using the applet viewerimport java.applet.Applet;
import java.awt.Graphics;public class AppletEx extends Applet {@Override
public void paint(Graphics g) {
g.drawString("Static Drawing Applet", 20, 20);
}}/*
* <applet code="AppletEx" width=300 height=100>
* </applet>
*/
Output:
How to run the code:
c:>javac AppletEx.java
c:>appletviewer AppletEx.java
Code Example 2:
// Java program to run the applet
// using the applet viewerimport java.applet.Applet;
import java.awt.Graphics;public class AppletEx extends Applet {@Override
public void paint(Graphics g) {
g.drawString("MS Dhoni", 20, 20);
}}/*
* <applet code="AppletEx" width="300" height="100">
* </applet>
*/
Output:
How to run the code:
C:>javac AppletEx.java C:>appletviewer AppletEx.java
Types of Applets: Local and Remote
Java applets can be classified into two types based on where they are stored and accessed: local and remote.
Local Applets: Characteristics and Use Cases
Local applets exist on the user’s own machine, and they do not require an Internet connection to work.
Characteristics:
Stored locally on the user’s system.
Accessed directly from the local file system.
Do not require internet access.
Use Cases:
Used for offline applications.
Suitable for environments with limited or no internet access.
Ideal for testing and development purposes.
Remote Applets: Characteristics and Use Cases
Remote applets are stored on an application server and may be accessed across a Web-based network. When necessary, they are ‘downloaded’ to the user’s browser.
Characteristics:
Stored on a remote server.
Accessed via the internet.
Downloaded and executed in the user’s browser.
Use Cases:
Used in various internet-related applications and services.
This format is ideal for presenting dynamic content to users irrespective of their geographical location.
Most appropriate for those Web applications that necessitate real-time updates and/or interactions.
Advantages and Disadvantages of Using Java Applets
Java applets are advantageous in several ways but are not without their demerits. Let’s explore both aspects.
Benefits of Integrating Applets into Web Pages
Improved Interactivity
Dynamic Content
Reduced Server Load
Cross-Platform Compatibility
Applets can help develop a more engaging user interface. They let us create games, animations, and even tools with which people can interact.
Applets can change the content of Web pages in real time, making the Web more dynamic.
As applets are implemented on the client side of an application, they help share the load of the server side, resulting in enhanced application performance.
Applets can be run on any operating system as long as a JVM executes them. This ensures that they provide consistent results across multiple platforms.
Drawbacks and Limitations of Applets
High Resource Consumption
Limited Browser Support
Security Risks
Development Complexity
Declining Popularity
Different applets may have large requirements for the system’s resource consumption, which may cause performance problems, especially with challenging or less efficient applets.
Unfortunately, as many modern browsers no longer support many of the newest Java applets for security measures, they are not very useful in today’s web design.
Applets have had some issues concerning security complications. Executing arbitrary code on a client machine can always lead to some kind of risk.
Creating applets is rather difficult, for example, for people who do not have previous experience in Java. Debugging and maintaining applets can also be very complex, primarily because popular development tools and libraries are not available.
As we know, with the availability of modern web technologies like HTML5, JavaScript, and, recently, web assembly, applet usage is not required. These newer technologies have added features of better performance, enhanced security, and compatibility with browsers.
Conclusion
In this blog, the applet life cycle in Java, that is, the stages when it is created, running, and when it self-terminates, have been discussed. Java applets are integrated into Web sites and serve as multimedia and interactive content. Still, they are less used because of the security issues and appearance of new Web based technologies, such as HTML5 and JavaScript. I have provided working examples and talked about the use of the browser and JVM, as well as the merits and demerits of using applets. Although the application applets are not so much present today, we need to know their life cycle to support old systems and have an understanding of web development progress.
FAQs
What is the purpose of the init() method in the applet life cycle?
The init() method initialises the applet, setting up necessary resources and variables. It is called once when the applet is first loaded.
When is the start() method called in the applet life cycle?
The start() method is called after the init() method and each time the applet's HTML page is loaded or refreshed. It contains the main functionality of the applet.
How does the stop() method function in the applet life cycle?
The stop() method pauses the applet's execution. It is typically invoked when the applet's window is minimised or when the user navigates away from the page.
What is the role of the destroy() method in the applet life cycle?
The destroy() method performs cleanup tasks, releasing resources before the applet is terminated and removed from memory. It is called once when the applet is about to be destroyed.
Are Java applets still widely used in web development?
No, Java applets are no longer widely used due to security concerns and the rise of modern web technologies like HTML5, JavaScript, and WebAssembly, which offer better performance and security.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.