Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
その中にクラスと年齢のフィールドがあります。しかし、オブジェクトの年齢を変更すると、クラスの他のすべてのオブジェクトも変更されます。
private static int age;
誰でもこの問題を解決できますか?
静的変数はクラス変数なstaticので、変数宣言から次のように削除します。
static
private int age;
Java docs に従って:
宣言に static 修飾子があるフィールドは、静的フィールドまたはクラス変数と呼ばれます。オブジェクトではなく、クラスに関連付けられています。クラスのすべてのインスタンスは、メモリ内の 1 つの固定位置にあるクラス変数を共有します。