class Base
{
static int a = 1;
}class Sub extends Base
{}
class Simple
{
public static void main(String[] args)
{
Base b = new Base();
Sub s = new Sub();
System.out.println("s.a= "+s.a);
s.a=2;
System.out.println("b.a= "+b.a);
System.out.println("s.a= "+s.a);
}
}
输出的结果居然是
s.a= 1;
b.a =2;
s.a =2;也就是说子类和父类共一个 a变量了?如果把static int a = 1;改成int a = 1;结果就会显然不同了。
这是为什么?难道static变量只有在内存中只有一份拷贝 即使发生了继承关系 子类也还是用父类的那一份static变量的拷贝???是这样的吗?
我把基础类型改为引用类型,更容易理解。如下:package test;
class Student{
}
class Base
{
static Student student = new Student();
}class Sub extends Base
{}
class StaticTest1
{
public static void main(String[] args)
{
Base b = new Base();
Sub s = new Sub();
System.out.println("s.a= "+s.student);//对象的字符串表示形式
s.student=new Student();
System.out.println("b.a= "+b.student);
System.out.println("s.a= "+s.student);
}
}
使用new关键字的对象存储在heap区中,即所谓的堆。垃圾回收器针对的就是这个区域的对象进行回收。也是我们需要关注是否有内存泄露的关键点
局部变量即在函数中申明的变量存储在stack区中,即所谓的栈。方法结束了也就被自动回收了,所以局部变量无需关心是否存在内存泄露的问题。
静态变量或全局变量存储在JVM的全局数据区,所以是所有实例共享的。这样就能理解为什么static的变量在所有实例对象中总是只有一份拷贝的原因了