黑马程序员技术交流社区

标题: 静态类修饰成员变量的特征,看代码!!!~~ [打印本页]

作者: lockwood    时间: 2015-5-6 22:23
标题: 静态类修饰成员变量的特征,看代码!!!~~
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 修饰成员变量以后,静态成员变量会在对象之前加载进来,并且,静态的成员变量可以实现数据的共享。
作者: 天若有晴    时间: 2015-5-6 22:31
看看楼主的,static修饰符的作用
作者: Melo    时间: 2015-5-6 23:59
static这块总是搞混
作者: 18463730277    时间: 2015-5-7 00:11
static 也起到复用的效果了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2