如果一个成员变量使用了static关键字,那么这个变量不再属于对象自己,而是属于所在的类。也就是说多个对象共享同一份数据。
例如:
package 类和对象;
public class Student {
private int id;
private String name; // 姓名
private int age; // 年龄
static String room; // 所在教室
private static int idCounter = 0; //学号计数器,每当new了一个新对象的时候,计数器++
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
this.id = ++idCounter;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package 类和对象;
public class Democ06 {
public static void main(String[] args) {
Student two = new Student("黄蓉",16);
two.room="101教室";
System.out.println("姓名:"+two.getName()+",年龄:"
+two.getAge()+",教室:"+two.room+",学号:"+two.getId());
Student one = new Student("郭靖",19);
System.out.println("姓名:"+one.getName()+",年龄:"
+one.getAge()+",教室:"+one.room+",学号:"+one.getId());
}
}
运行结果如下:
注:如果反过来把郭靖放前面,教室用one.room,学号仍然不变。