目录
static静态
静态的内容在内存中是保留一份的,并且各个对象之间进行共享。
特点:
1.数据共享
2.属于类,并不属于对象
3.优先于对象产生的
public class Person3 {
String name;
// String country;
static String country;
String address;
public Person3(String name, String address) {//这里的String country,就不需要了
this.name = name;
// this.country = country;
this.address = address;
}
public static void main(String[] args) {
Person3 p1 = new Person3("鲁正婷", "八大胡同");//"大清",
Person3 p2 = new Person3("曹正云", "朝阳门");//"大清",
//大清灭亡
//这样修改太过麻烦了
// p1.country="民国";
// p2.country="民国";
//使用static静态
//p1.country="民国";//此时修改一个即可达到效果。 //不推荐这样使用静态变量
Person3.country = "民国";//推荐使用类名去访问静态的内容
System.out.println(p1.country);
System.out.println(p2.country);
}
}
关于静态构造器,通用构造器和构造方法:
通用构造器,静态构造器 创建对象的过程(简单提及)
1.静态构造器
2.通用构造器
3.构造方法-->创建对象
由于创建对象的过程是在静态加载完成之后,在静态方法和静态块里不能使用this