0

I'm creating an array of different animal objects from the same super class and then using a for loop I'm retrieving a description of each animal using a method from another class but when I assign each new animal to the array it changes the name and type of each to that of the last animal. How do I get it to stop changing the parameters for each animal?

Here's my code now:

I have constructors for each type of animal listed with different descriptions for each type.

Animal[] family = new Animal[5];

family[0] = new Dog("Wrex", "dog");
family[1] = new Wolf("Fenrir", "wolf");
family[2] = new Snake("Kaa","snake");
family[3] = new Snake("Nagini", "snake");
family[4] = new Chameleon("David", "chameleon");

for(int i = 0; i < 5; i++)
{
    family[i].describe();
}

This is what it prints out:

David is a chameleon.

David barks.

David moves on four legs.

David gets food from its owner.

David eats by chewing.

David is a chameleon.

David howls.

David moves on four legs.

David gets food by hunting in packs.

David eats by chewing.

Edit

My describe method is different for each animal class but they all follow this format:

public void describe()
{
    System.out.println(Chameleon.getName()+ " is a " +Chameleon.getType()+ ".");
    makeSound();
    move();
    getFood();
    eatFood();
}

Edit 2

Here is a rough outline of each class and subclass

public class Animal {

private static String name;
private static String type;

public Animal(String name, String type)
{
    Animal.name = name;
    Animal.type = type;
}

public static String getName() {
    return name;
}

public void setName(String name) {
    Animal.name = name;
}

public static String getType() {
    return type;
}

public void setType(String type) {
    Animal.type = type;
}

public class Mammal extends Animal {

public Mammal(String name, String type)
{
    super(name, type);
}

}

reptile is the same as mammal

each of the last subclass looks like this with the describe method posted above

public class Chameleon extends Reptile{

public Chameleon(String name, String type)
{
    super(name, type);
}
user2929674
  • 3
  • 1
  • 3
  • 1
    We're going to have to see what .describe() contains in order to give you any answer. Also, the long list of output is unnecessary as long as your description of the problem is adequate. – Scott Solmer Oct 28 '13 at 20:56
  • I personally found the output helpful to understand the problem, but 2 or 3 items instead of 5 should be sufficient. – O. R. Mapper Oct 28 '13 at 20:56
  • 1
    can you show the `describe` method? – Vikrant Goel Oct 28 '13 at 20:58
  • Have you replaced `Chameleon` in your `describe` methods with the appropriate animal? Also, can you please show the implementation of `Chameleon.getName()`, as it does not seem to be an instance method that could return the values passed to the constructor? – O. R. Mapper Oct 28 '13 at 21:03
  • Can you give a rough structure of your classes? So `describe()` is in `Chameleon` or in `Animal`? – Vikrant Goel Oct 28 '13 at 21:04
  • Please add the definition of Animal and Chameleon. – Slugart Oct 28 '13 at 21:11

2 Answers2

1

static !!! remove the static keyword. That's the only problem. Read more about it here

use this and change things accordingly

private String name; 
private String type;
Community
  • 1
  • 1
Vikrant Goel
  • 654
  • 6
  • 20
  • The assignment requires static on the type variable. Also removing static from the instance variables gives me errors in all of my subclasses under mammal and reptile. – user2929674 Oct 28 '13 at 21:17
  • yes I know and that's why you have to change your code accordingly. Thing is, when you make a variable static, now no matter how many different objects you make for that class, the same variable will be used for all the objects. I don't think you can achieve what you want to with static variables. Maybe Check your problem statement again? – Vikrant Goel Oct 28 '13 at 21:19
  • Okay I got the code to work thanks for your help and information about static variables. – user2929674 Oct 28 '13 at 21:39
0

Variables name and type are declared as static in class "Animal". Therefore they are shared by all instances of the "Animal" class.

Juha Palomäki
  • 26,385
  • 2
  • 38
  • 43