您的位置:首页 > 其它

static属于class类的,不是属于某个对象的

2018-01-20 23:21 155 查看
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属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: