The Java Tutorials' Weblog

pageicon Tuesday Jul 15, 2008

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

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed

« December 2009
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today

Feeds

Search this blog

Links

Weblog menu

Today's referrers

Today's Page Hits: 783