0

Sorry if the title was worded incorrectly, please change it if it's not quite what I describe here.

So I am creating a program to simulate some fictitious creatures, and I need many objects that all store different statistics about each individual creature with values that can all be checked at the same time. What would be the correct way to go about doing this?

For reference, here is the code that I have right now.

public class Model {

static int mfood;
static int mdefense;
static int mattack;
static int mhealth;
static int msize;
static int magg;
static boolean update = false;

public Model(int food, int meat, int defense, int attack, int health, int agg) {

        mfood = food;
        msize = size;
        mdefense = defense;
        mattack = attack;
        mhealth = health;
        magg = agg;

}

The problem is, of course, that I have variables that store the values that are put into the constructor that are changed each time the constructor is called. This works well if I have only one object running at a time, but I will have around a dozen, most likely. These values need to be called in other methods as well, such as

    public void Attack() {
    mhealth = (mhealth + mdefense) - mattack;
}

The mattack variable would be a different object from the mhealth and mdefense variables.

Thanks for any help I can get, I'm sure this is a simple fix, but I can't find the answer anywhere.

  • 5
    just remove the static modifier, the static modifier means the variables are present for every instance of a class, useful when in certain cases, but not in yours. – Zack Newsham Nov 30 '14 at 23:40
  • 2
    And preferably make them private and use [getters and setters](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters). – James Nov 30 '14 at 23:42
  • I would suggest you read a basic Java tutorial on what `static` means and how to use it. – Boris the Spider Nov 30 '14 at 23:46

1 Answers1

3

Your variables are static, meaning they exist at the class level, not the individual object level. This means no matter how many objects you create (new up), they all share the same variables. Remove the static keywords and each object will have it's own copy of the variables.

Mark Wade
  • 517
  • 1
  • 4
  • 15