static关键字的核心:修饰的成员不属于对象了,而属于类本身。
它可以修饰字段,方法,内部类。
特点:
1、static修饰的内容,附着类的加载而加载——当JVM把class字节码加载到虚拟机时,static修饰的成员已经自动的加载到了内存中了。
2、优先于对象的存在——在new对象之前,static加载字节码时已经自动加载到JVM了。
public class StaticDemo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
PersonStatic per=new PersonStatic();
per.pInfo();
}
}
class PersonStatic{
public static void pInfo() {
info();
}
public void info() {
System.out.println("Hello!");
}
}
//错误的原因:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method info() from the type PersonStatic
这也就是为什么static不能调用非static内容的原因。而非static方法,可以访问静态成员也可以访问非静态成员。
3、static修饰的成员被该类型的所有对象共享——根据该类创建出来的对象,都可以访问static成员,表面上是通过对象访问,其本质依然使用类名访问,和对象没有任何关系。
4、可以直接使用当前类的类名访问static成员——因为static修饰的成员属于类,不属于对象。
//static声明的属性为该类中所有对象共享
public class StaticDemo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
PersonStatic per=new PersonStatic();
per.info();
//使用类名称修改static属性内容
PersonStatic.country="北京";//类属性调用——类名称 static 属性
per.info();
}
}
class PersonStatic{
static String country="成都";
public void info() {
System.out.println("城市"+country);
}
}
// 结果:城市:成都
// 城市:北京
//static声明方法
public class StaticDemo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
PersonStatic per1=new PersonStatic("张三",18);
PersonStatic per2=new PersonStatic("李四", 20);
System.out.println("————————修改前————————");
per1.info();
per2.info();
System.out.println("_________修改后————————");
PersonStatic.setCountry("上海");//使用类名称调用static方法
per1.info();
per2.info();
}
}
class PersonStatic{
private String name;
private int age;
private static String country="成都";
public PersonStatic(String name,int age) {
this.name=name;
this.age=age;
}
//修改static方法
public static void setCountry(String c) {
//country=c;
//或者
PersonStatic.country=c;
}
public static String getCountry() {
return country;
}
public void info() {
System.out.println("姓名:"+this.name+"年龄:"+this.age+"城市:"+country);
}
}
//————————修改前————————
//姓名:张三年龄:18城市:成都
//姓名:李四年龄:20城市:成都
//_________修改后————————
//姓名:张三年龄:18城市:上海
//姓名:李四年龄:20城市:上海
什么时候使用static关键字???
被所有对象共享时,节省空间,没必须每个对象都存储一份,可以直接使用类名调用。
static的应用
1、统计类有多少实例化对象
public class StaticDemo06 {
public static void main(String[] args) {
// TODO Auto-generated method stub
StaticDemo d1=new StaticDemo();
StaticDemo d2=new StaticDemo();
StaticDemo d3=new StaticDemo();
}
}
class StaticDemo{
static int num=0;
public StaticDemo() {
num++;
System.out.println("产生了"+num+"个实例化对象");
}
}
//结果:
/*产生了1个实例化对象
产生了2个实例化对象
产生了3个实例化对象*/
2、为对象自动编号
public class StaticDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new Demo().getName());
System.out.println(new Demo("张三").getName());
System.out.println(new Demo().getName());
}
}
class Demo{
private String name;
private static int num=0;
public Demo() {
num++;
this.name="Demo_"+num;
}
public Demo(String name) {
//this.name=name;
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Main方法
public static void main(String[] args) {}
解释:
public:表示此方法可以被外部访问。
static:表示此方法可以由类名直接调用。
void:主方法是程序的起点,所有不需要任何返回值。
main:执行时默认寻找main()。
String arg[]:表示运行的参数。参数传递的默认 形式为“java名称 参数1 参数2 …”。
arg[:]是一个数组。