2

我对静态字段的理解是,它的最后一个赋值是该字段在类中的任何位置的值。但是,我的理解显然并不可靠,因为我希望倒数第二个打印语句打印 30,而不是 6。请帮助我理解为什么它打印 6。

public class Whatever 
{
static int x = 2;
static int z;

public Whatever()
{
    z = x*3;
}

public static void main(String[] args) 
{
    Whatever foo = new Whatever();

    System.out.println(foo.z); //prints 6

    Whatever.x = 10;

    System.out.println(foo.x); // prints 10
    System.out.println(foo.z); // prints 6 WHY?!?!

    Whatever bar = new Whatever();

    System.out.println(bar.z); // prints 30 as expected
}
}
4

7 回答 7

2
Whatever.x = 10; // This only sets your "x" to 10.

System.out.println(foo.x); // prints 10
System.out.println(foo.z); // prints 6 - because x is 10, but z is still 6

Whatever bar = new Whatever(); // Now z = x*3 is called and z becomes 30.

System.out.println(bar.z);

只是z=x*3当您为x.

于 2013-03-25T03:24:10.557 回答
1

静态变量之所以这样称呼,是因为它们静态地位于内存中。当您实例化您的 foo 对象时,构造函数将 x 乘以 3,并将结果分配给 z。

当你的主要方法开始时,

x = 2 且 z 尚未实例化。

当 foo 被实例化时,

x = 2 和 z = x * 3。由于 x 现在是 2,所以 z = 2 *3 = 6

然后将 x 分配为 10。 Z 仍然是 6,因为在调用 bar 对象之前不会分配 z。

如果您希望在那个时间点是 30,您将不得不再次调用 z = x * 3。

改变变量 x 不会自动改变 z 的值

于 2013-03-25T03:24:32.280 回答
1

要理解这一点,您需要知道什么static意思。在 Java 中,static变量位于类级别。另一种思考方式是,该类的所有实例都看到相同的东西。因此,在您的示例中,无论您创建了多少Whatever对象(即使您创建了 NONE!),都只有一个x,只有一个z

那么,让我们来看看吧……

// Executes z = x*3, setting z to 6 (as you expect)
Whatever foo = new Whatever();
System.out.println(foo.z); //prints 6

// Sets x to 10 (remember, x is static, you only have one of them!)
Whatever.x = 10;
System.out.println(foo.x); // prints 10

// Because since the last time you've created a Whatever, 
// nothing has changed z!
System.out.println(foo.z); // prints 6 WHY?!?!

// Now you've created a new Whatever, and z = x*3 gets executed
// in the constructor again
Whatever bar = new Whatever();


System.out.println(bar.z); // prints 30 as expected

我希望这会有所帮助!

于 2013-03-25T03:25:17.893 回答
0

因为 z = x*3 在构造方法中。编写时调用构造函数方法

Whatever foo = new Whatever();

所以当你分配

Whatever.x = 10; 

构造方法没有再次调用

于 2013-03-25T03:29:40.053 回答
0

看看这里

当从同一个类蓝图创建多个对象时,它们每个都有自己不同的实例变量副本。(...)

有时,您希望拥有所有对象共有的变量。这是通过静态修饰符完成的。声明中带有 static 修饰符的字段称为静态字段或类变量。

于 2013-03-25T03:23:11.857 回答
0

让 c 一行一行地发生什么

公共静态无效主要(字符串[]参数){

Whatever foo = new Whatever(); // new is a operator to create an object. when the object is created it will try to call constructor . No constructor, it will fail to create the object. so Now the constructor is called. It calculates x*3 and puts the value to z.
System.out.println(foo.z); //prints 6. Here value of z is read printed. This is not method invocation. This is just reading value of z 
Whatever.x = 10; // This is an assignment statement. It just puts the value into x
System.out.println(foo.x); // prints 10 Just the values read 
System.out.println(foo.z); // prints 6 WHY?!?! // Here to value of z is read. If you see. now alteration of value is done on z. 
Whatever bar = new Whatever(); // this recalculates and puts the value on to z. That is why you are seeing change when you print z after this statement.
System.out.println(bar.z); // prints 30 as expected
System.out.println(foo.z); // Should also print 30 now. thats what static does

}

希望这可以帮助

于 2013-03-25T03:51:38.527 回答
-1

静态变量在不同的实例中只有一个实例。

这里

首先

Z 为 2

你打电话时

public Whatever()

Z 改为 6

之后你改变了 X

但这对 Z 没有影响

所以值为 6

谢谢

于 2013-03-25T03:25:10.327 回答