父类:
package test;
public class father {
public static int s=0;
}
子类1:
package test;
public class son1 extends father{
public son1()
{
s=1;
}
}
子类2:
package test;
public class son2 extends father{
public son2()
{
s=2;
}
}
测试
package test;
public class Test {
private static son1 s1 = new son1();
private static son1 s11 = new son1();
private static son2 s2 = new son2();
public static void main(String[] args) {
// TODO Auto-generated method stub
s1.s=1;
s11.s=3;
System.out.print("son1:"+s1.s+"\n");
System.out.print("son11:"+s11.s+"\n");
System.out.print("son2:"+s2.s);
}
}
输出:
结论:
静态变量永远都只占用一块内存,所有的对象都共享一个静态变量,只要有一个地方改变,则其他地方都要改变。
转载于:https://blog.51cto.com/rainlee/1303472