Direct Communication Between Applets, through Shared Data

Within the browser all applets run on a single Java Virtual Machine. When two applets use the same class, they share the static fields of that class. Static fields can thus be used to pass information between applets, whether they appear in the same Web page or not. Below is an example of two communicating applets:


Example code:
import java.applet.Applet;
import java.awt.Graphics;

public class Concur extends Applet {

    String s;

    public synchronized void init() {
	if (MyInit.status)
	    s = "Already Initialized";
	else
	    s = "Not Yet Initialized";
	MyInit.status = true;
    }

    public void paint(Graphics g) {
	g.drawString(s, 50, 25);
    }
}
public class MyInit {
    public static boolean status = false;
}