Applet中的Static变量问题
下面是我写的Applet,其中声明了两个Static型的变量,在Applet中声明的Static型变量好象和在别的类中声明的不一样。它不是该类所有实例所公用的。
package apptest;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Random;
public class App extends Applet implements Runnable
{
private boolean isStandalone = false;
static String msg = null;
static int k = 0;
Thread t = null;
boolean stopFlag;
//Get a parameter value
public String getParameter(String key, String def)
{
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public App()
{
}
//Initialize the applet
public void init()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void start()
{
t = new Thread(this);
stopFlag = false;
t.start();
}
public void run()
{
for(;;)
try{
Thread.sleep(3000);
repaint();
}catch(Exception e)
{
e.printStackTrace();
}
}
public void paint(Graphics g)
{
g.drawString(msg,k*10,k*10);
k++;
}
//Component initialization
private void jbInit() throws Exception
{
setBackground(Color.cyan);
setForeground(Color.red);
Random rand = new Random();
Integer in = new Integer(rand.nextInt(100));
msg = in.toString();
}
//Get Applet information
public String getAppletInfo()
{
return "Applet Information";
}
public void stop()
{
stopFlag = true;
t = null;
}
//Get parameter info
public String[][] getParameterInfo()
{
return null;
}
}
下面是相应的HTML文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>
HTML Test Page
</title>
</head>
<body>
apptest.Applet will appear below in a Java enabled browser.<br>
<applet
codebase = "."
code = "apptest.App.class"
name = "TestApplet"
width = "800"
height = "800"
hspace = "0"
vspace = "0"
align = "middle"
>
</applet>
</body>
</html>
我相继打开两个上面的HTML文档,由于我在Applet中输出的字符串和它输出的位置都是静态变量msg和k,我又利用线程调用重画函数,所以我期望的结果是第一个打开的HTML页面应该在第二个HTML页面打开时改变显示,跟第二个页面一模一样,可实际上它门不一样,说明它们用的静态变量msg和k是不同的值。这有是怎么回事呢?