static关键字——>看见static关键字,就与对象无关。静态,表示公有的含义。
1.1 修饰属性,类属性,类变量
1.2 修饰方法,类方法,工具方法
1.3 static修饰代码块,静态代码块
1.4 static修饰内部类,静态内部
先行eg
public class Static {
public static void main(String[]args){
Person per=new Person();
per.name="显平";
per.age=3;
per.high=1.8;
per.soleThickness=0.3;
per.country="bb国";
per.show();
Person per2=new Person();
per2.name="bb强";
per2.age=5;
per2.high=2.1;
per2.soleThickness=0.8;
per2.country="bb国国";
per2.show();
per.country="bb";
}
}
class Person{
String name;
int age;
double high;
double soleThickness;
static String country;
void show(){
System.out.println("name="+name+",age="+age+",high="+high+",鞋底高="+soleThickness+",country="+country);
}
}
运行结果为
name=显平,age=3,high=1.8,鞋底高=0.3,country=bb国
name=bb强,age=5,high=2.1,鞋底高=0.8,country=bb国国
再定义一个新对象
Person per3=new Person();
per3.country="bb国大王";
per3.show();
运行结果为
当一个实例变量被static关键字修饰,他就表示类的属性,该类所有对象共享这个属性。
static修饰的属性在JVM的方法区中储存,所有该类对象共享此属性。
static修饰的属性,直接通过类名称就可以访问,无需通过对象访问。



