Applet Life Cycle

Applet life cycle methods explained.

In java programming applet serves as the dynamic web application that interacts with the user on Internet and based on the input process the request.

While coding an applet understanding the applet life cycle methods is very important and here we will discuss four life cycle methods in Applet:

Methods are init, start, stop, and destroy are the applet life cycle methods. The following example will explain all the method execution better:

Import java.applet.*;

Import java.awt.*;

/*<applet code=”AppletDemo” width=”300″ height=”300″></applet>*/

public class AppletDemo extends Applet

{

            public void init()

            {

                        System.out.println(”init called”);

            }

            public void start()

            {

                        System.out.println(”start called”);

            }

            public void stop()

            {

                        System.out.println(”stop called”);

            }

            public void destroy()

            {

                        System.out.println(”destroy called”);

            }

}

In the above example necessary packages are imported and the comment for the Applet width and height is also coded.

Class is declared public and inherits the Applet parent class.

When you compile and execute or test the program using Appletviewer the output shown on the command prompt initially will be:

Init called start called

Here, init method is called only once during the execution of the applet. Start method is called every time the applet is viewed and maximized.

Now, minimize the applet window and the output will be:

Init called start called stop called

Every time applet is minimized stop method is called.

Now, if you close the applet window and the output will be:

Init called start called stop called destroy called.

Destroy method is used for garbage collection, init method is used for initialization when applet is loaded and start, stop are the applet working methods.

Thus, these four methods are known as applet life cycle methods in java.

Leave Your Response