0
String type cant be resolved to a variable (short program)
ERROR(Amber cant be resolved to a variable) public class Fish { String typeOfFish; int friendliness; public Fish(){ this.typeOfFish = "Unknown"; friendliness = 3; } public Fish(String t, int f){ typeOfFish = t; friendliness = f; } int getFriendliness(){ return friendliness; } Fish amber = new Fish(Amber,5); // WHY "Amber" CAN'T BE HERE Fish james = new Fish(); Fish nicestFish(Fish a, Fish b){ if(a.getFriendliness()> b.getFriendliness()) return a; else return b; } }
2 Réponses
+ 4
Look at your second constructor of Fish()
The first parameter is a String.
The second is an integer.
Fish amber = new Fish("Amber", 5);
+ 1
> WHY "Amber" CAN'T BE HERE?
because you create inside Fish -> new Fish -> new Fish ... in infinite loop
// use main() instead
public class Program {
public static void main(String[] args) {
Fish amber = new Fish("Amber", 5);
Fish james = new Fish();
}
}