staic是属于class类的,不是属于某个此类所创建的某个对象引用的。所以当你在debug时会发现,在Variables栏中并不会找到当前引用对象中static所修饰的变量。
测试代码块
package yu.bai.array;
public class BerylliumSphere {
private static long counter;
private final long id = counter++;
public String toString(){
return "Sphere" + id;
}
public static long getCounter() {
return counter;
}
public long getId() {
return id;
}
package yu.bai.array;
public class ContainerComparison {
public static void main(String[] args) {
BerylliumSphere[] spheres = new BerylliumSphere[10];
for (int i = 0; i < 5; i++) {
spheres[i] = new BerylliumSphere();
System.out.println(spheres[i].getCounter() + "," + spheres[i].getId());
}
}
}
当执行spheres[0] = new BerylliumSphere()时,debug运行的Variables栏中只有类BerylliumSphere的id属性,并没有显示counter属性。
本文通过一个具体的示例解释了static变量的特点,即它属于类而非特定对象实例。并通过一段Java代码演示了如何使用static变量,并展示了在调试过程中,static变量不会出现在特定对象实例的变量列表中。
4800

被折叠的 条评论
为什么被折叠?



