Some usage of static keyword

1. The static keyword modifies member variables

  The member variable uses the static keyword, then the variable no longer belongs to the object, but belongs to the class it is in. Multiple objects share the same data.

  Implement a counter in the entity class

public class Student {
    
    
    private Integer stuId;//id,每次new一个实现自增长
    private String stuName;
    private static int idCounter = 0;//计数器,多个对象共享同一份数据

    public Student() {
    
    
        this.stuId = ++idCounter;//每次调用构造方法,实现+1
    }

    public Student(String stuName) {
    
    
        this.stuId = ++idCounter;//每次调用构造方法,实现+1
        this.stuName = stuName;
    }

    public static int getIdCounter() {
    
    
        return idCounter;
    }

    public static void setIdCounter(int idCounter) {
    
    
        Student.idCounter = idCounter;
    }

    public Integer getStuId() {
    
    
        return stuId;
    }

    public void setStuId(Integer stuId) {
    
    
        this.stuId = stuId;
    }

    public String getStuName() {
    
    
        return stuName;
    }

    public void setStuName(String stuName) {
    
    
        this.stuName = stuName;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "stuId=" + stuId +
                ", stuName='" + stuName + '\'' +
                '}';
    }
}

Test code
Insert picture description here

Two, static keyword modification member method

  The static keyword modifies a member method, then it becomes a static method. The static method does not belong to the object, but belongs to the class.
  There is no static modification method, the object must be created before it can be used.

1. Static method, call directly with the class name
2. Static method in this class, the class name can be omitted

  Static cannot directly access non-static
  memory. First, there is static content and then non-static content.
  Static methods cannot use this (this represents the current object, who is the current object by whom to call)

Code:


public class MyClass {
    
    
    int num ;
    static  int numStatic;

    public void method(){
    
    
        System.out.println("普通的成员方法");
        System.out.println(num);//成员方法可以访问成员变量
        System.out.println(numStatic);//成员方法可以访问静态变量
    }

    public static void staticMethod(){
    
    
        System.out.println("这是一个静态方法");
//        System.out.println(num); 静态方法不能直接访问成员变量
        System.out.println(numStatic);
    }
}

public class StaticMethod {
    
    
    public static void main(String[] args) {
    
    
        MyClass myClass = new MyClass();
        myClass.method();//通过对象调用成员方法
        MyClass.staticMethod();//通过类名调用
        staticMethod();
    }

    public static void staticMethod(){
    
    
        System.out.println("本类中的静态方法");
    }

}

Three, static code block

  When this class is first used, the static code block is executed only once, and the static content always takes precedence over the non-static, so the static code block is executed before the construction method.
  Used to assign values ​​to static member variables at once

public class Person {
    
    
    static {
    
    
        System.out.println("静态代码块");
    }

    public Person(){
    
    
        System.out.println("构造方法执行");
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/zeduo2525/article/details/109284225