java 16.静态static

static

静态static关键字修饰成员

在一个类中,如果使用static关键字来修饰类属性(成员变量),那这个变量就属于类,类里面的所有对象都可以共享这个变量。

什么意思呢?我们看一下代码就明白了:

现在有一个类Student

package static_demo;

public class Student {
    private String name;
    private  int age;
    static String room;

    public Student() {
        System.out.println("调用空Student");
    }

    public Student(String name, int age) {
        System.out.println("调用带参Student");
        this.name = name;
        this.age = age;
    }

    public String getName() {
        System.out.println("调用getName方法");
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        System.out.println("调用getAge方法");
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static String getRoom() {
        System.out.println("调用getRoom方法");
        return room;
    }

    public static void setRoom(String room) {
        Student.room = room;
    }
}

然后我们下面这个函数调用Student类实例化两个Student成员对象one和two、并且定义one.room= "101教室",这里two可以不声明而直接使用!这就是因为在Student类中我们通过static来修饰room这个变量,room不是one对象的,虽然他对其做了声明,但是room是属于Student类的,不管创建几个Student的成员对象,成员之间都是共享一个room,这个room属于Student类。

package static_demo;

public class staticField {
    public static void main(String[] args) {
        Student one = new Student("郭靖",19);
        one.room = "101教室";
        System.out.println("姓名:" + one.getName() + ",年龄:" + one.getAge() + "所在教室:" + one.room);
        Student two = new Student("黄蓉",16);
        System.out.println("姓名:" + two.getName() + ",年龄:" + two.getAge() + "所在教室:" + one.room);
    }
}

在这个例子里,没有执行到空Student对象public Student()


下面我们实现这样一个效果:每次new Student对象都做一个加一操作,让学号递增。

也很简单,Student类代码如下

package static_demo;

public class Student {
    private int no;  //学号
    private String name;
    private  int age;
    static String room;
    private static int noCounter = 0;  //学号计数器

    public Student() {
        System.out.println("调用空Student");
        noCounter++;
    }

    public Student(String name, int age) {
        System.out.println("调用带参Student");
        this.name = name;
        this.age = age;
        this.no = ++noCounter;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public static int getNoCounter() {
        return noCounter;
    }

    public static void setNoCounter(int noCounter) {
        Student.noCounter = noCounter;
    }

    public String getName() {
        System.out.println("调用getName方法");
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        System.out.println("调用getAge方法");
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static String getRoom() {
        System.out.println("调用getRoom方法");
        return room;
    }

    public static void setRoom(String room) {
        Student.room = room;
    }
}

调用类的代码

package static_demo;

public class staticField {
    public static void main(String[] args) {
        Student one = new Student("郭靖",19);
        one.room = "101教室";
        System.out.println("姓名:" + one.getName()
                + ",年龄:" + one.getAge() + "所在教室:" + one.room + ",学号:" + one.getNo());
        Student two = new Student("黄蓉",16);
        System.out.println("姓名:" + two.getName()
                + ",年龄:" + two.getAge() + "所在教室:" + two.room + ",学号:" + two.getNo());
        Student oyf = new Student("欧阳锋",56);
        System.out.println("欧阳锋的学号是:" + oyf.getNo());
        Student yg = new Student("杨过",14);
        System.out.println("杨过的学号是:" + yg.getNo());
        Student xln = new Student("小龙女",16);
        System.out.println("小龙女的学号是:" + xln.getNo());
    }
}

输出

调用带参Student
调用getName方法
调用getAge方法
姓名:郭靖,年龄:19所在教室:101教室,学号:1
调用带参Student
调用getName方法
调用getAge方法
姓名:黄蓉,年龄:16所在教室:101教室,学号:2
调用带参Student
欧阳锋的学号是:3
调用带参Student
杨过的学号是:4
调用带参Student
小龙女的学号是:5

静态static内存图

image

静态代码块

静态代码块的格式:

public class 类名称{
    static {
        //静态代码块的内容
    }
}

特点:

  • 当第一次用到本类时,静态代码块执行唯一的一次。

  • 静态内容总是优先于非静态,所以静态代码比构造方法先执行。

静态代码的典型用途:

用来一次性地对静态成员变量进行赋值。

Person类

package static_demo;

public class Person {
    static {
        System.out.println("静态代码块执行!");
    }
    public Person(){
        System.out.println("构造方法执行!");
    }
}

调用类

package static_demo;

public class P_staticDemo {
    public static void main(String[] args) {
        Person a = new Person();
        Person b = new Person();
    }
}

输出

静态代码块执行!
构造方法执行!
构造方法执行!

posted @ 2021-09-18 01:08  我是一言  阅读(43)  评论(0编辑  收藏  举报