class TestStatic {
static int a= 1; //静态变量 a = 1;
}
写一个测试类class UseTestStatic {
public static void main(String[] args) {
//创建两个对象
TestStatic testStatic = new TestStatic();
TestStatic testStatic1 = new TestStatic();
//对象1的 a 输出 为1
System.out.println(testStatic1.a);
//对象 重新赋值其a之后
testStatic.a = 5;
//对象 1 的 a 值同样为 5 了
System.out.println(testStatic1.a);
}
}
本文通过一个简单的Java示例,展示了如何使用静态变量,并说明了当多个对象共享同一个静态变量时,改变其中一个对象的静态变量值如何影响其他对象。
508

被折叠的 条评论
为什么被折叠?



