The Java Tutorials' Weblog
Class Variables and the Applet
Has this ever happened to you? In your applet, you create a global variable by defining a public non-final static field. When you are testing your applet the field doesn't have the value you expect it to have. This can happen when a browser uses the same JVM to run multiple applet instances and all are writing to the same global variable.You can't make any assumptions on how a browser will implement the JVM. But if you are using JDK 6, you can take advantage of the new class loader cache applet parameter. Specifying "false" for this parameter will ensure that each applet instance will get its own unique set of statics.
Another possible solution available in JDK 6 is to launch the applet directly from a JNLP file. Applets launched through this Java Plug-In mechanism will never share static state.
Thanks to Yvon Rozijn who submitted this question to the tutorial feedback alias.
-- Sharon Zakhour
Posted at 12:36PM Jul 15, 2008 by The Java Tutorial Team | Comments[0]
Tuesday Jul 15, 2008