1、声明static属性
static关键字主要用来定义属性和方法
写一个常见的代码:
class Person{
private String name;
private int age;
String city="成都";
public Person(String name,int age) {
this.name=name;
this.age=age;
}
//setter、getter略
public String getInfo() {
return "姓名:"+this.name+"、年龄:"+this.age+"、城市:"+this.city;
}
}
public class Hello {
public static void main(String args[]) {
Person per1 = new Person("张三",65);
Person per2 =new Person("李四",66);
Person per3 =new Person("王五",67);
System.out.println(per1.getInfo());
System.out.println(per2.getInfo());
System.out.println(per3.getInfo());
}
}
如果人数有8000人,并且更改城市,那么你会去写8000多行代码吗?
我肯定不会去这样做,工作量太大,所有我们需要将city这个属性定义为公共属性。
class Person{
private String name;
private int age;
static String city="成都";
public Person(String name,int age) {
this.name=name;
this.age=age;
}
//setter、getter略
public String getInfo() {
return "姓名:"+this.name+"、年龄:"+this.age+"、城市:"+this.city;
}
}
public class Hello {
public static void main(String args[]) {
Person per1 = new Person("张三",65);
Person per2 =new Person("李四",66);
Person per3 =new Person("王五",67);
per1.city="北京";
System.out.println(per1.getInfo());
System.out.println(per2.getInfo());
System.out.println(per3.getInfo());
}
}
结果是这样的:
此时就会发现city的属性内容都发生了改变,所以这是一个公共属性。
访问static属性需要通过程序里的最高代表来进行访问,也就是通过类来进行访问。所以static属性可以直接用类名称来进行访问。
Person.city="北京";
static属性虽然在类当中,但不受到类实例化对象的控制,所以static属性可以在没有实例化对时使用。
class Person{
private String name;
private int age;
static String city="成都";
public Person(String name,int age) {
this.name=name;
this.age=age;
}
//setter、getter略
public String getInfo() {
return "姓名:"+this.name+"、年龄:"+this.age+"、城市:"+this.city;
}
}
public class Hello {
public static void main(String args[]) {
System.out.println(Person.city);
Person.city="北京";
Person per1 = new Person("张三",65);
System.out.println(per1.getInfo());
}
}
2、声明static方法
static关键字也可以进行方法的定义,static方法的主要特点在于其可以直接由类名称在没有实例化对象的情况下进行调用。
package cn.edu.nsu.tl;
import java.util.Scanner;
class Person{
private String name;
private int age;
private static String city="成都";
public Person(String name,int age) {
this.name=name;
this.age=age;
}
public static void setCity(String c) { //static方法
city = c;
}
//setter、getter略
public String getInfo() {
return "姓名:"+this.name+"、年龄:"+this.age+"、城市:"+this.city;
}
}
public class Hello {
public static void main(String args[]) {
Person.setCity("北京");
Person per = new Person("张三",45);
System.out.println(per.getInfo());
}
}
这个时候对于程序而言方法就有了两种:static方法、非static方法;
- static方法只允许调用static属性或static方法;
- 非static方法允许调用static属性或static方法;
所有的static定义的属性和方法都可以在没有实例化对象的前提下使用,而所有的非static定义的属性和方法必须要有实例化对象的情况下使用。
static定义的方法或者属性都不是你代码编写之初所需要考虑的内容,只有在回避实例化对象调用并且描述公共属性情况下才会考虑使用static定义的方法或者是属性。
3、static应用案例
案例:实现实例化对象的统计,每一次创建新的实例化对象时都可以进行一次统计
class Book{
private String name;
private static int count = 0;
public Book() {
this("NOTITLE-"+count++);
}
public Book(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class Hello {
public static void main(String args[]) {
Book book = new Book("Java");
System.out.println(book.getName());
System.out.println(new Book().getName());
}
}
为了避免在没有设置name属性时,内容为空的情况,设置了一个无参构造方法。