static关键字
1. 使用static关键字修饰一个属性
声明为static的变量实质上就是全局变量
2.使用static关键字修饰一个方法
通常在一个类中定义一个方法为static,那就是说,无需本类的对象就可以调用这个方法
3.使用static关键字修饰一个类(内部类)
例子:
新建一个Role类
public class Role {
private String name;
private String country;
// 无参数构造方法
public Role() {
}
// 带参数的构造方法
public Role(String name, String country) {
this.name = name;
this.country = country;
}
// getter and setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
// 输出格式方法
public String getInfo() {
return "我的名字是" + name + ",我来自" + country;
}
}
主函数输出
public static void main(String[] args) {
Role beibei = new Role("刘备", "蜀国");
Role yunchang = new Role("云长", "蜀国");
Role feifei = new Role("张飞", "蜀国");
System.out.println(beibei.getInfo());
System.out.println(yunchang.getInfo());
System.out.println(feifei.getInfo());
}
这种方法的存储方式为:
从上图可见,每个对象都有相同country = 蜀国,这种存储方式会浪费内存,因此需要引用static关键字。
对以上代码进行修改
public class Role {
private String name;
//private static String country = "蜀国";//静态变量,也就是全局变量
static String country = "蜀国";//将private去掉后,可以直接使用
// 无参数构造方法
public Role() {
}
// 带参数的构造方法
public Role(String name) { //构造方法将不再需要country
this.name = name;
}
// getter and setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/* 静态变量将不再需要getter and setter 方法
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
*/
//静态方法
public static void setCountry(String country){
//this.country = country 不能这样使用,this表示当前对象的意思
Role.country = country; //用类名
}
//静态方法
public static void from(){
System.out.println("我来自"+country)
}
// 输出格式方法
public String getInfo() {
return "我的名字是" + name + ",我来自" + country;
}
}
输出
public static void main(String[] args) {
Role beibei = new Role("刘备");
Role yunchang = new Role("云长");
Role feifei = new Role("张飞");
System.out.println(beibei.getInfo());
System.out.println(yunchang.getInfo());
System.out.println(feifei.getInfo());
//调用静态变量
System.out.println(Role.country);
//调用静态方法
Roel.from();
}
用这种方法可以得到相同的结果,但是对线的存储方式会发生变化
将country = 蜀国 存放到静态方法区里,成为对象共用的区域,对象可以调用静态方法区里的信息.
public static void main(String[] args) {
Role beibei = new Role("刘备");
Role yunchang = new Role("云长");
Role feifei = new Role("张飞");
System.out.println(beibei.country);
System.out.println(yunchang.country);
System.out.println(feifei.country);
beibei.country = "魏国";
System.out.println(beibei.country);
System.out.println(yunchang.country);
System.out.println(feifei.country);
System.out.println(Role.country);//尽量用类名来调用静态变量(用类名调用的变量一定是静态的)
}
可以直接用beibei.country来得到country
如果修改beibei.country = “魏国”,输出结果是beibei.country,yunchang.country , feifei.country的结果全部是魏国,这就说明了被static修饰后的country已经成为了全局变量,是对象共有的。
但是尽量不要用对象名(beibei)去调用静态数据(被static修饰后的属性),直接使用类名调用(Role.country)用类名调用的变量一定是静态的
使用static的情况:
所有对象有共同的属性或方法的时候(如:country = “蜀国”)
static 关键字小结
- 静态变量或方法不属于对象,依赖类;
- 静态变量是全局变量,生命周期从类被加载一直到程序结束
- 静态变量只能存储一份,在静态方法区中存储;
- 静态变量是本类所有对象共享的
- 建议不要使用对象名去调用静态数据,直接使用类名调用(尽量不要使用beibei.country 使用 Role.country来调用)
- static修饰一个方法,那么该方法属于类,不属于对象,直接用类名调用(Role.from());
- 静态方法不能访问非静态属性和方法,只能访问静态的(静态方法是类被加载后便存在的,而非静态的数据需要new 一个对象才会有,静态方法的生命周期长,静态方法存在的时候,非静态数据不一定存在,所以不能访问)
- 不能以任何方式引用this或super