2

静的フィールドについての私の理解は、その最後の割り当ては、クラス内のどこにある場合でも、そのフィールドの値がどうなるかということです。ただし、最後から2番目の印刷ステートメントが6ではなく30を印刷することを期待していたため、私の理解は明らかに確固たるものではありません。なぜ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は割り当てられないため、Zはまだ6です。

その時点で30になると予想される場合は、z = x*3を再度呼び出す必要があります。

変数xを変更しても、zの値は自動的に変更されません

于 2013-03-25T03:24:32.280 に答える
1

これを理解するには、その意味を知る必要がありますstatic。Javaでは、static変数はクラスレベルにあります。別の見方をすれば、そのクラスのすべてのインスタンスが同じものを見るということです。したがって、あなたの例では、Whatever作成するオブジェクトの数に関係なく(NONEを作成した場合でも)、1つだけが存在し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

こちらをご覧ください

同じクラスのブループリントから多数のオブジェクトが作成される場合、それぞれにインスタンス変数の独自のコピーがあります。(...)

すべてのオブジェクトに共通の変数が必要な場合があります。これは、静的修飾子を使用して実行されます。宣言に静的修飾子が含まれているフィールドは、静的フィールドまたはクラス変数と呼ばれます。

于 2013-03-25T03:23:11.857 に答える
0

z = x*3がコンストラクターメソッドに含まれているためです。コンストラクターメソッドは、次のように記述するときに呼び出されます

Whatever foo = new Whatever();

だからあなたが割り当てるとき

Whatever.x = 10; 

コンストラクターメソッドが再度呼び出されなかった

于 2013-03-25T03:29:40.053 に答える
0

行ごとに何が起こるかをcしましょう

public static void main(String [] args){

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

静的変数には、異なるインスタンス間で1つのインスタンスしかありません。

ここ

初めに

Zは2でした

電話するとき

public Whatever()

Zが6に変更されました

その後、Xを変更しました

しかし、それはZに影響を与えませんでした

したがって、値は6です。

ありがとう

于 2013-03-25T03:25:10.327 に答える