class Student {
private String name;
private int age;
private static String country;
public Student(){}
public Student(String name, int age, String country) {
this.name = name;
this.age = age;
this.country = country;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void show() {
System.out.println(name + "****" + age + "****" + country);
}
}
class StaticDemo {
public static void main(String[] args) {
Student s1 = new Student("Lockwood", 20, "US");
Student s2 = new Student("Lewis", 30);
Student s3 = new Student("FishHeadJone", 50);
s1.show();
s2.show();
s3.show();
}
}
s2,s3会自动输出country的值 为US。这是因为static 修饰成员变量以后,静态成员变量会在对象之前加载进来,并且,静态的成员变量可以实现数据的共享。 |
|