Home » JAVA » Applet life cycle with exmaple
applet life cycle
applet life cycle

Applet life cycle with exmaple

Java applet derive features from the class Applet. whenever an applet is design, it experience series of changes from initialization to destruction. different- different stages of an applet life cycle are characterize in the figure applet life cycle.

How Applet life cycle works

  • Various states, an applet, undergoes between its object creation and object removal (when the job is over) is known as life cycle. Each state is represented by a method. There exists 5 states represented by 5 methods. That is, in its life of execution, the applet exists (lives) in one of these 5 states.
  • These methods are known as “callback methods” as they are called automatically by the browser whenever required for the smooth execution of the applet. Programmer just write the methods with some code but never calls.
  • Following are the methods. init() method start() method paint() method stop() method destroy() method
  1. init() method
  2. start() method
  3. paint() method
  4. stop() method
  5. destroy() method

applet life cycle methods in Brief

  1. init(): The applet’s voyage starts here. In this method, the applet object is created by the browser. Because this method is called before all the other methods, programmer can utilize this method to instantiate objects, initialize variables, setting background and foreground colors in GUI etc.; the place of a constructor in an application. It is equivalent to born state of a thread
  2. start(): In init() method, even through applet object is created, it is in inactive state. An inactive applet is not eligible for microprocessor time even though the microprocessor is idle. To make the applet active, the init() method calls start() method. In start() method, applet becomes active and thereby eligible for processor time.
  3. paint(): This method takes a java.awt.Graphics object as parameter. This class includes many methods of drawing necessary to draw on the applet window. This is the place where the programmer can write his code of what he expects from applet like animation etc. This is equivalent to runnable state of thread.
  4. stop(): In this method the applet becomes temporarily inactive. An applet can come any number of times into this method in its life cycle and can go back to the active state (paint() method) whenever would like. It is the best place to have cleanup code. It is equivalent to the blocked state of the thread.
destroy(): This method is called just before an applet object is garbage collected. This is the end of the life cycle of applet. It is the best place to have cleanup code. It is equivalent to the dead state of the thread.
Applet life cycle Program Example

About Bhavin Shiroya

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.