5

私のJavaプログラムでは、最近の値を変数に保存する必要があり、コードは次のとおりです

public class Exmp2 
{

        int noOfInstances;
    public Exmp2() 
    {
        noOfInstances++;
    }
    public static void main(String[] args){
        Exmp2 e1=new Exmp2();
        System.out.println("No. of instances for sv1 : " + e1.noOfInstances);

        Exmp2 e2=new Exmp2();
        System.out.println("No. of instances for sv1 : "  + e2.noOfInstances);
        System.out.println("No. of instances for st2 : "  + e2.noOfInstances);

        Exmp2 e3=new Exmp2();
        System.out.println("No. of instances for sv1 : "  + e3.noOfInstances);
        System.out.println("No. of instances for sv2 : "  + e3.noOfInstances);
        System.out.println("No. of instances for sv3 : "  + e3.noOfInstances);
    }
}

出力は 1 2 2 3 3 3 のはずですが、1 1 1 1 1 1 になっています。解決策を教えてください。

4

9 に答える 9

7

noOfInstances変数を として宣言しますstatic

static int noOfInstances;

ではないため、そのインスタンスstaticごとにnew Exmp2()aが作成され、デフォルト値はです。noOfInstances0

于 2013-03-28T06:10:15.397 に答える
3

noOfInstances静的であることを宣言する必要があります

    static int noOfInstances;

それ以外の場合、 によって作成されたすべての新しいインスタンスは、再び 0 から始まるnew独自の値 を持ちます。noOfInstances

于 2013-03-28T06:11:31.740 に答える
2

変数を静的として宣言する必要があります。static変数は常に最近の値を格納し、その静的変数は次の場所に格納されます。static pool

static int noOfInstances;
于 2013-04-04T09:13:13.217 に答える
2

noOfInstances should be declared static. For example:

static int noOfInstances;

This might be an interesting read. It should have an example with a similar situation as yours:

static keyword

In short, static literally makes a variable shared among instances of a given class. If you have a variable that is not static every instance will have its own private value for that variable. While a statically declared variable will have the same value in all instances of a class.

于 2013-03-28T06:15:09.080 に答える
2

変数は宣言する必要がありますstatic

なんで?現在、変数noOfInstances静的ではなく、作成するクラスのインスタンスごとに noOfInstances 変数も作成され、常に値が 1 になるため、変数を静的に宣言すると、このクラスのすべてのインスタンス間で共有され、正しい価値。

クラスがロードされると静的変数が作成され、すべてのインスタンスで共有されます。

于 2013-03-28T06:15:31.400 に答える
2

以下のように静的にしnoOfInstances ます。

static int noOfInstances; // static are shared among all objects created.

e1.noOfInstances代わりに呼び出す必要はありませんExmp2.noOfInstancesを呼び出すことができます

インスタンス (非静的) 変数はオブジェクトにコピーされますが、静的変数はオブジェクトにコピーされません。static はクラス レベルです。すべてのオブジェクトがそれを見ることができます。

于 2013-03-28T06:24:24.247 に答える
1

インスタンス数に静的変数を使用する方法については、以下の例を参照してください。

package com.stackoverflow.test;

public class Exmp2 {

    static int noOfInstances;

    public Exmp2() {
        noOfInstances++;
    }

    public static void main(String[] args) {
        System.out.println("No. of instances at this point "
                + Exmp2.noOfInstances);
        Exmp2 e1 = new Exmp2();
        System.out.println("No. of instances at this point "
                + Exmp2.noOfInstances);
        Exmp2 e2 = new Exmp2();
        System.out.println("No. of instances at this point "
                + Exmp2.noOfInstances);
        Exmp2 e3 = new Exmp2();
        System.out.println("No. of instances at this point "
                + Exmp2.noOfInstances);
    }
}
于 2013-04-03T09:01:37.570 に答える
1

新しいオブジェクトをインスタンス化するときに 0 を割り当てるたびnew Exmp2();に、スコープがクラス レベルにシフトするようにします。noOfInstancesnoOfInstancesstatic

于 2013-03-28T06:16:45.120 に答える