Microsoft KB Archive/227210

= INFO: When are Java Applet Methods init, start, stop, and destroy Called in IE3 Vs IE4 and Greater? =

Article ID: 227210

Article Last Modified on 6/14/2006

-

APPLIES TO


 * Microsoft Java Virtual Machine
 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 3.0
 * Microsoft Internet Explorer 3.01
 * Microsoft Internet Explorer 3.02
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 4.01 Service Pack 1
 * Microsoft Internet Explorer 4.01 Service Pack 2

-



This article was previously published under Q227210



SUMMARY
This article describes exactly when the Java applet methods init, start, stop, and destroy are called in the different versions of Internet Explorer.

Internet Explorer versions 4.0 and later changed behavior with respect to these methods when compared to IE3. This article explains how to make your Java applet code run consistently in all versions of IE.



MORE INFORMATION
The change in behavior that occurs in IE4 and later was made mainly in order to save memory.

In IE3, the applet method init is only called if the applet page isn't found in the cache and start is always called when moving to the page. Furthermore, stop is always called when leaving the page and destroy is called only when the browser exited or the page was flushed out of the cache.

In IE4 and later, the applet methods init and start are always called when moving to an applet page, and stop and destroy are always called when leaving the page.

If an applet's init function contains code that takes a long time to process and must always be available to the start method, make this code static in order to extend its life beyond the time the applet is seen in the browser (important for IE4 and greater), speeding up subsequent visits to the applet. If the computation results are not needed instantly when the start method is executed, it's recommended to start the long-running processing in another thread.

The following code demonstrates this issue and shows what needs to be done in order to mimic the IE3 behavior in IE4 and later. Running this code and inspecting the output from the various versions of IE clearly shows the difference in behavior since IE3. Note when running this code that the IE progress bar finishes much faster when revisiting the applet page compared to when it is first visited and the lengthy processing in the init method is called.

Cut and paste the following code and save it to a file called Applet1.java. Compile this source code into a class file using Visual J++ 6.0 or the Microsoft SDK for Java: import java.applet.*; public class Applet1 extends Applet { //To keep track if init has been called static Boolean m_initialized = Boolean.FALSE; // Object that takes long time to create or populate static String m_hugestring = ""; public void init {   synchronized (m_initialized) {     System.out.println("Output from using init method"); if (m_initialized.booleanValue) return;

System.out.println("Lengthy processing starting in init method");

// Do some lengthy processing to populate the m_hugestring for ( int i=0; i <  10000; i++) m_hugestring += "% ";

m_initialized = Boolean.TRUE; } }  public void stop {   System.out.println("Output from using stop method"); } public void start {   System.out.println("Output from using start method: m_hugestring length = "                         + m_hugestring.length); } public void destroy {   System.out.println("Output from using destroy method"); } } Cut and paste the following code and save it to an HTML file, for example one called Applet1.htm:  Can't run an applet -- this browser doesn't support Java.  Compare the output that results from running the above HTML code, moving off the applet page, moving back to the applet page, then moving off the applet page again and closing IE.

Using IE3 the above steps will produce the following output (as found in the javalog.txt file): Output from using init method Lengthy processing starting in init method Output from using start method: m_hugestring length = 20000 Output from using stop method Output from using start method: m_hugestring length = 20000 Output from using stop method Output from using destroy method Following the same steps using IE4 or greater will produce the following output (as found in the Java Console window): Output from using init method Lengthy processing starting in init method Output from using start method: m_hugestring length = 20000 Output from using stop method Output from using destroy method Output from using init method Output from using start method: m_hugestring length = 20000 Output from using stop method Output from using destroy method

