-
所属不同
1)静态变量属于类,所以也称为类变量
2)成员变量属于对象,所以也称为实例变量(对象变量)
- 内存中位置不同
1)静态变量存储于方法区的静态区
2)成员变量存储于堆内存
- 内存出现时间不同
1)静态变量随着类的加载而加载,随着类的消失而消失
2)成员变量随着对象的创建而存在,随着对象的消失而消失
- 调用不同
1)随着类的加载而加载
2)优先于对象存在
3)被类的所有对象共享
4)可以通过类名调用
6.静态方法与静态变量好处:
public class Student {
static {
System.out.println("Student 静态代码块");
}
{
System.out.println("Student 代码块");
}
public Student() {
System.out.println("Student 构造方法");
}
}
public class StudentTest {
static {
System.out.println("aaaa");
}
public static void main(String[] args) {
System.out.println("我是main方法");
Student s1 = new Student();
Student s2 = new Student();
}
}