java learning - Static static keyword modifications member

/ *
If a member variable using the static keyword, so the variable no longer belong to the object itself, but belong to the class is located. Multiple objects share the same data.
* /

public class Demo01StaticField {
    public static void main(String[] args) {
        Student one = new Student("郭靖", 19);
        one.room = "101教室";
        System.out.println("姓名:" + one.getName()
                + ",年龄:" + one.getAge() + ",学号:" + one.getId()
                + ",教室:" + one.room);

        Student two = new Student("黄蓉", 16);
        System.out.println("姓名:" + two.getName()
                + ",年龄:" + two.getAge() + ",学号:" + two.getId()
                + ",教室:" + two.room);
    }
}

Student category

public class Student {
    private String name;//姓名
    private int age;//年龄
    private int id;//学号
    static String room;// 所在教室
    private static int idCounter = 0;// 学号计数器,每当new了一个新对象的时候,计数器++

    public Student() {
        this.id = ++idCounter;
    }

    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;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

``

Published 23 original articles · won praise 0 · Views 151

Guess you like

Origin blog.csdn.net/qq_44813352/article/details/104319193