Microsoft KB Archive/234364

= PRB: Sharing Static Variables Across Multiple Applets =

Article ID: 234364

Article Last Modified on 10/20/2003

-

APPLIES TO


 * Microsoft Java Virtual Machine
 * Microsoft Visual J++ 6.0 Standard Edition

-



This article was previously published under Q234364



SYMPTOMS
When you access a static variable in one class from other classes, there seem to be multiple instances of the static variable.



RESOLUTION
To share static variables across multiple classes, each class must be loaded by a common classloader. Classes loaded by different classloaders will each have their own copy of the static variables.

To ensure that the classes are loaded via a common classloader, make sure that the following conditions are met:
 * Classes must share the same codebase URL.
 * The pages that host the classes must come from the same security zone.
 * The same values must be specified in the ARCHIVE, CABBASE, NAMESPACE, and CABINETS tags for each class if those tags are used.

NOTE: Values are compared using a case-sensitive string-literal comparison, so the strings must match exactly.



STATUS
This behavior is by design.



Steps to Reproduce Behavior

 * 1) Compile the following four Java files into classes.
 * 2) Place the class files from the first two java files into a .zip file named AppletChatServer.zip and place the other two class files plus AppletChatClient$1.class into AppletChatClient1.zip and AppletChatClient2.zip, respectively.
 * 3) Move these .zip files into a subfolder named Classes.
 * 4) View each of the HTML samples in turn.

AppletChatServer.java import java.applet.*; import java.util.*;

public final class AppletChatServer { public static Vector v=new Vector; public static void register(AppletChatClient client){ if(!v.contains(client)) v.addElement(client); }   public static void broadcast(String msg){ for(Enumeration en=v.elements;en.hasMoreElements;) ((AppletChatClient)en.nextElement).receive(msg); } }

AppletChatClient.java import java.applet.*; import java.awt.event.*; import java.awt.*;

public class AppletChatClient extends Applet { TextArea ta=new TextArea; Button   bu=new Button("Send"); Panel    pa=new Panel; TextField tf=new TextField("Type your message here..."); public void init{ AppletChatServer.register(this); setLayout(new BorderLayout); add("South" ,bu); add("Center",pa); pa.setLayout(new BorderLayout); pa.add("South",tf); pa.add("Center",ta); bu.addActionListener(new ActionListener{           public void actionPerformed(ActionEvent e){                AppletChatServer.broadcast(tf.getText);                tf.selectAll;            }}); }

public void receive(String msg){ ta.append(msg+"\n"); } }

AppletChatClient1.java public class AppletChatClient1 extends AppletChatClient { public AppletChatClient1 { super; } }

AppletChatClient2.java public class AppletChatClient2 extends AppletChatClient { public AppletChatClient2 { super; } }

In the first of the two following HTML samples, the unpaired applets don't interact with each other. There are no errors, and everything seems like it will function normally, but each applet pair has it's own copy of the static variables.

test1.html  

 

 



 

 

</BODY> </HTML>

When you run this HTML sample, you'll see that everything is functioning as expected. The only change is that the ARCHIVE tags all match exactly.

test2.html <HTML> <BODY>

 </APPLET>

 </APPLET>

<BR>

 </APPLET>

 </APPLET>

</BODY> </HTML>

Keywords: kbjava kbprb KB234364

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.