Applets Lifecycle


  • When an Applet loads:
    • The browser creates an instance of the Applet (no-arg constructor is called)
    • The browser initializes it (init() is called)
    • The browser starts it (start() is called)

  • Leaving and returning to the page:
    • The browser stops it (stop() is called)
    • The browser starts it (start() is called)

  • Reloading the HTML file:
    • stop()
    • destroy() (to clean-up resources)
    • Constructor (create a new Applet object)
    • init()
    • start()


    Detailed Description:

           "Let's go through the sequence of methods that a Java applet executes when you load a web page containing it. First, when your applet is loaded, the browser will load your class. Actually, the Java virtual machine will do it under direction from the browser, and the browser will invoke your no-argument constructor. That's the first thing that takes place, and that's only done once for your applet. So if you have a code or an execution that you wish to do only once, put it in the constructor.

           The next thing that the browser will do is called the init() method. This initializes the user interface typically. In specific instances, in the browser or what's called the applet viewer, it is possible to have the init() method called multiple times. As a result, put those things in the init() method that can be done more than once but are typically considered initializations.

           After initialization, the browser invokes the start() method. This is where you have a chance to actually start up your applet and do whatever is necessary. You are not always required to fill out each of these methods. After the browser initializes your applet, the start() method is called. Most of the time, it's used for starting up secondary threads. The reason is that the start() method will be invoked every time your web page is displayed or re displayed."