可能重复:
隐藏类的实例变量
我有以下课程
抽象测试
public abstract class AbstractTest {
protected int testVar = 10;
}
测试
public class Test extends AbstractTest {
int testVar = 5;
public static void main(String[] args) {
AbstractTest test = new Test();
System.out.println(test.testVar);//Prints 10
Test secondTest = new Test();
System.out.println(secondTest.testVar);//Prints 5
}
}
为什么上面的程序打印10
第一种情况和5
第二种情况,尽管它是同一类的对象,即Test()
?
更新:
I am now confused about how memory is allocated to Object and its variables. As instance variable is getting changed based on Class which is behaviour of Static?
更新:1
Every object will have two variables so question of same memory allocation does not comes in to picture. Thanks.