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; } }

4th Apr 2020, 8:36 PM
Dzondzula
Dzondzula - avatar
2 Antworten
+ 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);
4th Apr 2020, 8:55 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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(); } }
4th Apr 2020, 10:48 PM
zemiak