Results 1 to 4 of 4
- 04-11-2009, 07:43 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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:
Rabbit 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; } }
Fox code is:Java Code:public class Rabbit extends Animal { public Animal newAnimal() { return new Rabbit(false); } }
So when I try to replace the instanceof() code in Animal with Rabbit.newAnimal I get that error saying:Java Code:public class Fox extends Animal { public Animal newAnimal() { return new Fox(false); } }
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; }
- 04-11-2009, 08:20 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
because you are trying to call newAnimal from the class, not an instance, you must define it as "static".
public static Animal newAnimal() { ... }
- 04-11-2009, 08:46 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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.
- 04-11-2009, 09:14 PM #4
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
-
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
non-static method cannot be referenced
By Taz_84 in forum New To JavaReplies: 19Last Post: 12-30-2008, 11:35 AM -
Method cannot be referenced from a static context - HELP
By jmorris in forum New To JavaReplies: 11Last Post: 11-19-2008, 03:13 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks