static静态变量
1.static
2.static 内存图
3.static的注意事项
- 静态方法不能调用非静态变量
- 静态方法不能调用非静态方法
静态成员,随着类的加载而加载,优先于对象先存在于内存当中
package a01staticdemo1;
/*
* 1.静态方法中只能访问静态
* 2.静态方法中没有this关键字
* 3.非静态方法可以访问所有
*
* */
public class Student {
String name;
int age;
static String teacherName;
//this:表示当前方法调用者的地址
//这个this 是由虚拟机赋值的
public void show1(Student this){
System.out.println("this" + this);
System.out.println(this.name + ", " + this.age + ", " + this.teacherName);
this.show2();
}
/*public void show1(){
System.out.println(name + ", " + age + ", " + teacherName);
//调用其他方法
show2();// = this.show2()
}*/
public void show2(){
System.out.println("show2");
}
public static void method(){
//System.out.println(this.age);静态方法不能访问非静态的
//this.show1();静态方法不能访问非静态的
System.out.println("静态方法");
}
}
package a01staticdemo1;
public class TestDemo {
public static void main(String[] args) {
Student.teacherName = "Adma";
Student stu1 = new Student();
stu1.name = "zhangsan";
stu1.age = 23;
stu1.show1();
System.out.println("==================");
Student stu2 = new Student();
stu2.name = "lisi";
stu2.age = 24;
stu2.show1();
}
}