C语言中静态变量只能被初始化一次,下次即使程序执行到初始化语句也会忽略,在java中也是一样,只是java在链接阶段对变量进行初始话的,而且即使即使后续在类的实例初始化阶段也不会再执行连接阶段的静态变量初始化过程,验证代码如下:
package com;
public class myThread{
/**
* @param args
*/
private static int i=2;
public void echo_i()
{
System.out.println(i);
}
public void run()
{
for(;i<10;i++)
{
System.out.println(this.hashCode()+”: “+i);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
myThread t = new myThread();
t.echo_i();
t.run();
myThread g = new myThread();
g.echo_i();
myThread j = new myThread();
j.echo_i();
}
}
结果如下:
2
1167165921: 2
1167165921: 3
1167165921: 4
1167165921: 5
1167165921: 6
1167165921: 7
1167165921: 8
1167165921: 9
10
10