Java Made Clear: Difference start vs init in Applet

Java Made Clear: Difference start vs init in Applet

start vs init Applet:

Like other concepts of Java like Thread, Servlet and Enterprise Bean, an Applet also comes with a life cycle. Different states in which an applet exist between its object creation and object garbage collection is known as life cycle. The methods involved in the life cycle of applet are init(), start(), paint(), stop() and destroy(). These methods are known as life cycle methods. All the methods are callback methods.

The Browser is the tool that executes the applet. It is the browser’s responsibility to provide the execution environment for applet. Browser is also should take care to call the life cycle methods at appropriate times.

Because all the life cycle methods defined in java.applet.Applet are concrete (non-abstract) methods, the Programmer need not override all and can override (use) what ever he thinks fit for his code. Generally, Programmer uses init() to initialize variables, paint() to display on applet window and destroy() to free the system resources like closing file streams and socket handles etc. Let us summarize start vs init differences.

Here, I discuss only two methods init() and start() and other methods usage with complete tutorial along figures can be read from Life Cycle of Applet that goes in 3 pages.

1. init() Method of start vs init

In this stage, the applet starts its journey. This method is called when the applet is loaded by the Browser for execution. In this method, the applet object is created by the applet. Here, the Programmer can initialize variables, create objects, load imags etc. The init() method is equivalent to a constructor of an application. This method is called only once in the life cycle. Here, the PARAM tags of HTML file are read with getParameter() method of Applet.

2. start() Method of start vs init

The start() method is called by init() method. For browser, there is another reason to call the start() method. When applet is brought back from minimize (seen in status bar) position. In minimize position, applet looses focus of the viewer. When the applet get focus, Browser calls first start() and then paint(). This method is called and executed multiple times whenever the applet gains focus (after loosing earlier).

Leave a Reply

Your email address will not be published. Required fields are marked *