Results 1 to 4 of 4
  1. #1
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default error: non-static method newAnimal() cannot be referenced

    This might be obvious error to all programmers but i am new to programming and cannot find out what is going on. I have 3 classes (Animal, Fox, and Rabbit). I can get the code to compile using instanceof() but I am trying to use polymorphism to replace certain code but cannot get it to compile. Here is the code:

    Animal code is:

    Java Code:
    public abstract class Animal
    {
       public Animal newAnimal(){
            Animal babyAnimal;
            if (this instanceof Rabbit){
                babyAnimal = new Rabbit(false);
            }else{
                babyAnimal = new Fox(false);
            }
            return babyAnimal;
        }
    }
    Rabbit code is:

    Java Code:
    public class Rabbit extends Animal
    {
      public Animal newAnimal() {
            return new Rabbit(false);
        }
    }
    Fox code is:

    Java Code:
    public class Fox extends Animal
    {
       public Animal newAnimal() {
            return new Fox(false);
        }
    }
    So when I try to replace the instanceof() code in Animal with Rabbit.newAnimal I get that error saying:

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

    so this is the new code i am trying to use in Animal in its place:

    Java Code:
    public Animal newAnimal(){
            Animal babyAnimal;
            if (Rabbit.newAnimal()){
                Rabbit.newAnimal();
            }else{
                Fox.newAnimal();
            }
            return babyAnimal;
        }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    because you are trying to call newAnimal from the class, not an instance, you must define it as "static".

    public static Animal newAnimal() { ... }

  3. #3
    alpdog14 is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    thanks emceenugget. i changed the two methods in Fox and Rabbit to static and changed the Animal class code to:

    public Animal newAnimal(){
    Animal babyAnimal;
    if (this instanceof Rabbit){
    babyAnimal = Rabbit.newAnimal();
    }else{
    babyAnimal = Fox.newAnimal();
    }
    return babyAnimal;
    }

    And now the error I get in both Fox and Rabbit classes are:

    "newAnimal() in *Rabbit* cannot override newAnimal() in Animal; overriding method is static"

    Please let me know what i am doing wrong.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    public class AnimalTest {
        public static void main(String[] args) {
            Fox fox = new Fox();
            Animal animal = fox.newAnimal();
            System.out.println("animal = " + animal.getClass().getName());
        }
    }
    
    abstract class Animal {
       public abstract Animal newAnimal();
    }
    
    
    class Rabbit extends Animal {
        boolean b;
    
        public Rabbit(boolean b) {
            this.b = b;
        }
    
        public Animal newAnimal() {
            return new Rabbit(false);
        }
    }
    
    class Fox extends Animal {
        boolean b;
    
        public Fox() {
            this(false);
        }
    
        public Fox(boolean b) {
           this.b = b;
        }
    
        public Animal newAnimal() {
            return new Fox(false);
        }
    }

Similar Threads

  1. Replies: 5
    Last Post: 03-21-2009, 09:32 PM
  2. non-static method cannot be referenced
    By Taz_84 in forum New To Java
    Replies: 19
    Last Post: 12-30-2008, 11:35 AM
  3. Replies: 11
    Last Post: 11-19-2008, 03:13 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •