The
Applet
class provides a framework for applet execution, defining methods that the system calls when milestones occur. Milestones are major events in an applet's life cycle. Most applets override some or all of these methods to respond appropriately to milestones.
init
MethodThe
init
method is useful for one-time initialization that doesn't take very long. Theinit
method typically contains the code that you would normally put into a constructor. The reason applets don't usually have constructors is that they aren't guaranteed to have a full environment until theirinit
method is called. Keep theinit
method short so that your applet can load quickly.
start
MethodEvery applet that performs tasks after initialization (except in direct response to user actions) must override the
start
method. Thestart
method starts the execution of the applet. It is good practice to return quickly from thestart
method. If you need to perform computationally intensive operations it might be better to start a new thread for this purpose.
stop
MethodMost applets that override the
start
should also override thestop
method. Thestop
method should suspend the applet's execution, so that it doesn't take up system resources when the user isn't viewing the applet's page. For example, an applet that displays an animation should stop trying to draw the animation when the user isn't viewing it.
destroy
MethodMany applets don't need to override the
destroy
method because theirstop
method (which is called beforedestroy
) will perform all tasks necessary to shut down the applet's execution. However, thedestroy
method is available for applets that need to release additional resources.
Note: Keep implementations of thedestroy
method as short as possible, because there is no guarantee that this method will be completely executed. The Java Virtual Machine might exit before a longdestroy
method has completed.