0

I am trying to operate a public method that is in an abstract class. I tried to operate that method from inside a public method that inside a public class that extends an other abstract class, but the compiler gave me:

"non-static method method_name() cannot be referenced from a static context"

What do I need in order to make it operate that method ?

public class Dingo extends Animal
{
    public void act()
    {
      if (kangarooCrossing())
      {
            Weapon.killAnimalMySquare();    //<<<<<<<<<< THE Problematic line
      }

      if(canMove()) 
            move();
      else 
            changeDirection();
    }
    // returns true if a Kangaroo is crossing.
    private boolean kangarooCrossing()
    {
        Actor kangaroo = getOneObjectAtOffset(0, 0, Kangaroo.class);
        if(kangaroo != null) {
            return true;
        }
        else {
            return false;
        }
    }
}

abstract class Weapon  extends Actor
{
    /**Kills an animal that steps on current square*/
     public void killAnimalMySquare()
    {
        Actor animal = getOneObjectAtOffset(0, 0, Animal.class);
        if(animal != null)
            getWorld().removeObject(animal);
    }
    /**returns true if an animal is crossing*/
    public boolean animalCrossing()
    {
         Actor animal = getOneObjectAtOffset(0, 0, Animal.class);
         if(animal != null) 
             return true;
         return false;
    }
}

thnx !!!

Master C
  • 1,536
  • 4
  • 14
  • 19

3 Answers3

1

It seems that method you invoke goal method from is static.

You need to create an instance of this class and call this method on instance.

Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
1

The public method you are creating in Child class shouldn't be a static method in order to call a method using this or super references.

EDIT based on your submitted code:

You cannot make a method call killAnimalMySquare from class Weapon like this:

Weapon.killAnimalMySquare();

You should call it like this:

killAnimalMySquare();

or

super.killAnimalMySquare();

or

this.killAnimalMySquare();

Weapon.killAnimalMySquare() would have been the correct syntax if killAnimalMySquare() was declared as static.

Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Pls see my EDIT section above. – anubhava May 23 '11 at 23:57
  • So I should declare killAnimalMySquare() as static ? because with your 3 given options the compiler says: "can't find symbol - method killAnimalMySquare()"... ?? – Master C May 24 '11 at 06:16
  • Method killAnimalMySquare() is defined in the abstract class Weapon and somehow I thought earlier that child class Dingo is extending Weapon but it is obviously not the case that's why compiler is complaining. Making killAnimalMySquare static will also not work since that method is calling other methods of Weapon or Actor classes. Moreover Weapon is abstract class so you can't even create an instance of that class. btw I believe there is no need for Weapon to be abstract since there is no abstract method in that class (unless Actor has some abstract method that is not implemented in Weapon). – anubhava May 24 '11 at 13:36
  • I don't want the customer to create an object from 'weapon'. So what should I do in order to solve this issue ? – Master C May 24 '11 at 13:46
  • There are ways to restrict clients creating instance of class Weapon like making its constructor private or making it an inner private class of Dingo. See this post for more info: http://stackoverflow.com/questions/1326230/avoid-instantiating-a-class-in-java or this discussion: http://www.coderanch.com/t/369821/java/java/Preventing-class-instantiating – anubhava May 24 '11 at 13:52
0

We need more of your code to give a more specific answer, but the general answer is that you're trying to call a non-static method from within a static method. You need to have an instance of your object created, and then use that object to call the non-static method.

Something like:

MyAbstractClass abstract = new MyDerivedClass();
abstract.my_overridden_method();
jwismar
  • 12,164
  • 3
  • 32
  • 44