You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何可修改Java静态变量的值?关于静态变量特性的疑问

为什么Java的静态变量可以被修改?

Great question—this is a super common point of confusion when learning about static variables, so let's unpack it step by step.

First, let's clarify the key properties of static variables you mentioned, because you're mixing up two separate concepts here:

  • "仅在程序执行开始时初始化一次,先于任何instance variables完成初始化": This refers to the initialization timing of the static variable. When your Java class is loaded by the JVM, the static variable is assigned its initial value exactly once—before any instances of the class are created, and before any instance variables are initialized. In your code, static int a =10; is this one-time initialization step. This doesn't mean the value can't be changed later, just that this initial assignment only happens once.

  • "由类的所有实例共享唯一副本": This means every instance of the class (and the class itself) references the same memory location for the static variable. So if you modify the value, every place that accesses a (whether via the class like Test.a or an instance like new Test().a) will see the updated value. This is exactly what your code is doing when you set a=20;—you're modifying that single shared value.

The confusion comes from thinking "initialized once" equals "can't be modified"—but those are totally separate ideas! The static keyword controls where the variable is stored, when it's initialized, and how it's shared. It doesn't make the variable immutable.

If you wanted a static variable that can't be modified after initialization, you'd add the final modifier, like this:

static final int a = 10;

Now trying to set a=20; would throw a compile-time error, because final enforces that the variable can only be assigned once (during initialization).

Let's extend your code to see the shared behavior in action:

class Test {
    static int a =10;
    public static void main(String args[]) {
        a=20;
        System.out.println("Test.a after modification: " + Test.a); // 输出20
        
        Test t1 = new Test();
        Test t2 = new Test();
        
        System.out.println("t1.a: " + t1.a); // 输出20
        System.out.println("t2.a: " + t2.a); // 输出20
        
        t1.a = 30;
        System.out.println("Test.a after t1 modifies it: " + Test.a); // 输出30
        System.out.println("t2.a after t1 modifies it: " + t2.a); // 输出30
    }
}

This shows that all references to a point to the same value—modifying it anywhere updates it everywhere, which aligns perfectly with the static variable's "shared unique copy" property.

To sum up: There's no conflict here. Static variables are initialized once and shared across all instances, but they're still mutable unless you add the final keyword to make them immutable.

内容的提问来源于stack exchange,提问作者Rahul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:53:12