Applet Life Cycle in Java

anand kumar

Every Java applet inherits a set of default behaviors from the applet class. As a result, when an applet is loaded, it undergoes a series of changes in its state.

  • Born on initialization state
  • Running state
  • Idle State
  • Dead or destroyed state

Initialization State

Applet enter the initialization state when it is first loaded. This is achieved by calling in init() method of Applet Class. The applet is born. At this stage, we may do the following, if required.

  • Create objects needed by the applet
  • set up initial values
  • Load images or fronts
  • set up colors

The initialization occur only once in the applets life cycle. To provide any of the behaviors mentioned above, we must override the init() method.

Running State

Applet enter the running state when the system call the start() method of Applet class. This occurs automatically after the applet is initialized. Starting can also if the applet is already in stopped state.

For example, we may leave the web page containing the applet temporarily to another page and return back to the page. This again starts the applet running. 

Note that, unlike init() method, the start() method may be called more than once. we may override the start() method to create a thread to control the applet.

Idle or Stopped State

An applet becomes idle when it is stopped form running, Stopping automatically when we leave the page containing the currently running applet. We can also do so by calling the stop() Method explicitly. if we use a thread to run the applet, then we must use stop() method to terminate the thread. We can achieve this by overriding the stop() method.

Dead State

An applet is said to be bead when it removed from memory. This occurs automatically by invoking the destroy() method when we quit the browser. Like initialization, destroying stage occurs only once in the applets life cycle. If the applet has created any resources, like thread, we may override the destroy() method to clean up these resources.