by Dinesh Thakur Category: AWT and Applets

Java applet inherits features from the class Applet. Thus, whenever an applet is created, it undergoes a series of changes from initialization to destruction. Various stages of an applet life cycle are depicted in the figure below:

Initial State

When a new applet is born or created, it is activated by calling init() method. At this stage, new objects to the applet are created, initial values are set, images are loaded and the colors of the images are set. An applet is initialized only once in its lifetime. It's general form is:

public void init() {
     //Action to be performed

 Applet Life cycle

Running State

An applet achieves the running state when the system calls the start() method. This occurs as soon as the applet is initialized. An applet may also start when it is in idle state. At that time, the start() method is overridden. It's general form is:

public void start() {
     //Action to be performed

Idle State

An applet comes in idle state when its execution has been stopped either implicitly or explicitly. An applet is implicitly stopped when we leave the page containing the currently running applet. An applet is explicitly stopped when we call stop() method to stop its execution. It's general form is:

public void stop() {
       //Action to be performed

Dead State

An applet is in dead state when it has been removed from the memory. This can be done by using destroy() method. It's general form is:

public void destroyed() {
     //Action to be performed

Apart from the above stages, Java applet also possess paint() method. This method helps in drawing, writing and creating colored backgrounds of the applet. It takes an argument of the graphics class. To use The graphics, it imports the package java.awt.Graphic.

Comparing Applets And Applications

Although Java applets are small applications and both applets and application are implemented by Java API, but there are some differences between applets and applications. Some of these differences are :
• An application's main class contains a unique static method called main. This main serves as the execution entry point for applications. In contrast, applets don't have a static main method. Instead, a method named init is called to get the applet started.
• Applications are the standalone programs which need Java Virtual Machine for its execution while an applet must be embedded in an HTML file and run under the control of Java-enabled web browser.
• When an application starts to run, no application object exists. In contrast, an applet cannot run until an applet object has created.
• An applet is embedded in HTML pages whereas an application has no support for HTML.
• By default, applications are not subjected to security restrictions, although the security manager can installed. On the other hand, because applets distributed over the web, so they are subject to security restrictions under the control of the web browser's security manager.
• Swing based applications extend the JFrame class whereas applets extend the JApplet class.
• An application can run with or without graphical user interface whereas an applet must run with a graphical user interface.
• When an applet starts running, it inherits and overrides the life cycle methods such as init(), start(), stop() and destroy(). The web browser calls these methods at various time during an applet's life. In contrast, applications have no life cycle methods.
• While it is natural for an application class to use a constructor method for initialization, this is not the case with applets. In the former case, initialization is implicit at object instantiation. In the latter case, applets cannot always initialize at instantiation, since some properties derived from its environment which determined after applet instantiation. As such, applet initialization is thus best via the init() method.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles