-1

I am trying to learn java. Forgive me if my concepts are not clear or very wrong. I am trying to create inheritance and polymorphism application.

I have created an array of Animals[5]. I am trying to add refrences of dog, cat to the array. I want it to hold Animals[0] = zooDog

I am getting error that cannot make a static reference to the non-static

I have create AnimalstestDrivve class

package animals;

public class AnimalstestDrive {

    public Animals[] myZoo = new Animals[5];
    int zooCounter = 0;

    public static void main(String[] args) {

        //Set animals array
        Dog zooDog = new Dog();
        addAnimals(zooDog);

        Cat zooCat = new Cat();
        addAnimals(zooCat);

    }

    public void addAnimals(Animals a){
        if ( zooCounter > 5 ){
            myZoo[zooCounter] = a;
            zooCounter++;
        }
        else
            System.out.println("Zoo is full");
    }
}

here is my Animals class

package animals;

public abstract class Animals {
    private String Name;
    private int Size; //Size on the scale 1 to 10

    public void eatFood(){
        System.out.println("I am eating food");
    }

    public void sleep(){
        System.out.println("I am sleeping now");
    }

    abstract public void makeNoises();

}

Simple dog, cat class

package animals;

public class Dog extends Animals {
    public void makeNoises(){
        System.out.println("Bow! bow!");
    }
}
  • It's a bad idea to name a class `Animals` if it's just one animal. Should be `Animal[] animals`. – Sergei Tachenov May 03 '15 at 18:29
  • Please [*search for error messages*](http://stackoverflow.com/search?q=cannot+make+a+static+reference+to+the+non-static+java) before asking a question. There are enough existing answers on SO for this exact problem. – user2864740 May 03 '15 at 18:29
  • possible duplicate of [Cannot Make Static Reference to Non-Static Method](http://stackoverflow.com/questions/4969171/cannot-make-static-reference-to-non-static-method) – user2864740 May 03 '15 at 18:30

3 Answers3

2

The main method (static) attempts to call the addAnimals method, whose declaration is non-static. You need to create an instance of the class first, then call the method on this instance

AnimalstestDrive testDrive = new AnimalstestDrive();
Dog zooDog = new Dog();
testDrive.addAnimals(zooDog);

See Understanding Class Members for more information

copeg
  • 8,290
  • 19
  • 28
0

You need to have an instance of the class AnimalstestDrive. Static means, that you don't need any instance of the class to use the class method, so if you would mark the addAnimals as static, You could use that method without creating an instance of AnimalstestDrive.

0

Because the method addAnimals is not static, you need to create an instance of AnimalstestDrive to use that function. When a method is not static, it is specific to an instance of that class. For example:

AnimalstestDrive atd = new AnimalstestDrive();
atd.addAnimals(new Dog()); // this will add animals to "atd"

If a method is static, it is not specific to an instance of the class, but the class itself.

If you put this method in the class AnimalstestDrive:

public static void staticMethod() {

}

You could only access it with AnimalstestDrive.staticMethod(), not atd.staticMethod().

More info on static methods here.

codecubed
  • 780
  • 7
  • 8