static关键字

发布于2020-08-14   539 次阅读


本文最后更新于2022年6月1日,已超过 1 年没更新!内容可能已失效,请自行测试。

声明static属性

static是一个关键字,这个关键字主要可以用来定义属性和方法

static定义属性

在一个类之中,所有的属性一旦被定义了,实际上内容都交由各自的堆内存空间保存

案例代码-定义一个程序类

class Country {
  private String name;
  private int age;
  static String country = "中华民国"; // 国家暂时不封装

  public Country(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getInfo() {
    return "姓名: " + this.name + "年龄: " + this.age + "国家: " + this.country;
  }
  // set,get忽略
}

public class StaticDemo {
  public static void main(String[] args) {
    Country A = new Country("张三", 20);
    Country B = new Country("李四", 24);
    Country C = new Country("王五", 25);
    A.country = "中华人民共和国";
    System.out.println(A.getInfo());
    System.out.println(B.getInfo());
    System.out.println(C.getInfo());
  }
}

static属性可以由类名称直接调用

static属性可以在没有实例化对象的时候使用

进行类设计的时候首选非static属性,考虑到公共信息的时候用static

声明static方法

static关键字也可以进行方法的定义

class Country2 {
  private String name;
  private int age;
  private static String country = "中华民国"; // 国家

  public Country2(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public static void setCountry(String c) {
    country = c;
  }

  public String getInfo() {
    return "姓名: " + this.name + "年龄: " + this.age + "国家: " + this.country;
  }
}

public class StaticDemo2 {
  public static void main(String[] args) {
    Country2.setCountry("中华人民共和国");
    Country2 per = new Country2("张三", 10);
    System.out.println(per.getInfo());
  }
}

这个时候对于程序而言,方法就有了两种:static方法和非static方法,这两个方法之间在调用上就有了限制

  • static方法只允许调用static属性或static方法
  • 非static方法允许调用static属性或static方法

所有static定义的属性和方法都可以在没有实例化对象的前提下使用

public class JavaStaticDemo {
  public static void main(String[] args) {

    new JavaStaticDemo().print();
  }

  public void print() {
    System.out.println("1111");
  }
}

static定义的方法或者属性都不是代码编写之初所需要考虑的内容,只有在需要回避实例化对象调用并且描述公共属性的情况下,才会考虑使用static定义的方法或属性

static应用案例

编写一个程序类,这个类可以实现实例化对象个数的统计,每一次创建新的实例化对象都可以实现一个统计操作

单独创建一个static属性,因为所有对象共享同一个static属性,构造方法中可以实现数据统计

class Book {
  private String title;
  private static int count = 0;

  public Book(String title) {
    this.title = title;
    count++;
    System.out.println("第" + count + "本新的图书创建出来");
  }
}

public class StaticDemo3 {
  public static void main(String[] args) {
    new Book("Java");
    new Book("Jsp");
    new Book("Spring");
  }
}

实现属性的自动命名处理

如果现在传递了title的属性,就是用传递的属性内容,如果没有传递title属性,则自动采用"NOTITLE-编号"的形式进行该属性内容的定义

class Book2 {
  private String title;
  private static int count = 0;

  public Book2() {
    this("NOTITLE-" + count++);
  }

  public Book2(String title) {
    this.title = title;
    // System.out.println("第" + count + "本新的图书创建出来");
  }

  public String getTitle() {
    return this.title;
  }
}

public class StaticDemo4 {
  public static void main(String[] args) {
    System.out.println(new Book2("Java").getTitle());
    System.out.println(new Book2("Jsp").getTitle());
    System.out.println(new Book2("Spring").getTitle());
    System.out.println(new Book2().getTitle());
  }
}

这样的好处是可以避免在没有设置title属性时内容为NULL的重复问题

=

一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。