Superclass discards already active objects

This is a bit complicated, but I will try to explain my problem. I created a program with a superclass ( RichIndustrialist ) of two subclasses ( PredecessorRichIndustrialist and one that I did not add) and 4 subclasses to these subclasses ( CrazyRichIndustrialist and 3 more). Now the program is too difficult to explain, but the problem is actually simple. My constructor is in a superclass, and each subclass uses it to initialize. Each time I create a new subclass object, such as CrazyRichIndustrialist , it discards all existing subclasses (from any subclass) to the value of the new object. I do not know how to fix this. Thanks in advance...

RichIndustrialist :

 package Mortal; import java.util.Random; public class RichIndustrialist implements Mortal { private static String Name; private static double holdings; private static int Alive; public RichIndustrialist(String Rich_Name, double Rich_holdings) { this.Name = Rich_Name; this.holdings = Rich_holdings; this.Alive = 1; } public int isAlive() { return (this.Alive); } public void setHoldings(double new_holdings) { this.holdings = new_holdings; } public double getHoldings() { return (this.holdings); } public String getName() { return (this.Name); } public void die() { this.Alive = 0; } public void getHeritage(double heritage) { this.holdings = this.holdings + heritage; } } 

PredecessorRichIndustrialist :

 package Mortal; import java.util.Arrays; public class PredecessorRichIndustrialist extends RichIndustrialist { private static String Name; private static double holdings; private RichIndustrialist[] successors = {}; private static int Alive; public PredecessorRichIndustrialist(String Rich_Name, double Rich_holdings) { super(Rich_Name,Rich_holdings); } public void die() { super.die(); } public void Inheritance(double holdings, RichIndustrialist[] successors) { int i = 0; while (i < successors.length) { int Alive = successors[i].isAlive(); System.out.println(Alive); if (Alive == 0) { removeSuccessor(successors[i]); i++; } else { i++; } } } public void addSuccessor(RichIndustrialist new_successor) { RichIndustrialist[] new_successors = new RichIndustrialist[successors.length + 1]; if (successors.length == 0) { new_successors[0] = new_successor; successors = new_successors; } else { for (int i = 0; i < successors.length; i++) { new_successors[i] = successors[i]; } new_successors[new_successors.length - 1] = new_successor; } this.successors = new_successors; } public void removeSuccessor(RichIndustrialist removed_successor) { RichIndustrialist[] new_successors = new RichIndustrialist[this.successors.length - 1]; int j = 0; for (int i = 0; i < this.successors.length; i++) { if (!this.successors[i].equals(removed_successor)) { new_successors[j] = this.successors[i]; } else { j--; } j++; } } public RichIndustrialist[] getSuccessors() { return successors; } } 

CrazyRichIndustrialist :

 package Mortal; import java.util.Random; public class CrazyRichIndustrialist extends PredecessorRichIndustrialist { private RichIndustrialist[] successors = {}; private static String Name; private static double holdings; private static int Alive; public CrazyRichIndustrialist(String Rich_Name, double Rich_holdings) { super(Rich_Name,Rich_holdings); } public void die() { super.die(); Inheritance(getHoldings(),getSuccessors()); } public void addSuccessor(RichIndustrialist new_successor) { super.addSuccessor(new_successor); } public void removeSuccessor(RichIndustrialist removed_successor) { super.removeSuccessor(removed_successor); } public void Inheritance (double holdings , RichIndustrialist[] successors) { super.Inheritance(holdings, successors); for (int i=0; i<successors.length-1; i++) { double random = new Random().nextDouble(); double amount = this.holdings * random; successors[i].getHeritage(amount); holdings = this.holdings - amount; } successors[successors.length-1].getHeritage(this.holdings); this.holdings = 0; } public String getName(){ return super.getName(); } public double getHoldings(){ return super.getHoldings(); } public RichIndustrialist[] getSuccessors(){ return super.getSuccessors(); } public void setHoldings(double new_holdings){ super.setHoldings(new_holdings); } public int isAlive() { return super.isAlive(); } public void getHeritage(double heritage) { super.getHeritage(heritage); } } 
+4
source share
4 answers

Most of your fields are static . This means that all instances of your classes have the same value. When you call the constructor, the static fields change, which affects all existing instances.

For instance:

 this.Name = Rich_Name; 

should be written:

 RichIndustrialist.Name = Rich_Name; 

You can read about the difference between instance members and class (or static) in this tutorial .

+7
source

The following fields must be declared non-static. When these fields are declared static, each instance of RichIndustrialist will share these fields and their assigned values. Declaring them as non-static allows each RichIndustrialist instance RichIndustrialist have its own copy of these fields, which is autonomous from other RichIndustrialist instances.

 private String Name; private double holdings; private int Alive; 

Here is a good description of static from a Java tutorial

Sometimes you want to have variables common to all objects. This is achieved using a static modifier. Fields that have a static modifier in the declaration are called static fields or class variables. They are associated with a class, not with any object. Each instance of a class uses a class variable that is in one fixed place in memory. Any object can change the value of class, but class variables can also be manipulated without instantiating the class.

+6
source

Your properties / variables are static. and we know that a static variable is distributed among all objects.

This is why the last object will replace the existing value of your variables

Sentence:

change your static modifier to instance modifier

WITH

 private static String Name; private static double holdings; private static int Alive; 

For

 private String Name; private double holdings; private int Alive; 

I am sure that your problem will be solved.

+3
source

You declare the Name member field in all your classes, you should only declare it in the super class and use its sub -classes (re).

In addition, you declared the field as static , all instances of your class will use the same field, which is probably not the way you expected, so delete the static part.

The same goes for all your member fields.

Note. Do not start member fields with capital: Name should be defined and used as Name . Class names, on the other hand, must start with capital! This is a common Java convention and simplifies and separates things.

+2
source

All Articles