This is my warrior class. I wanted to have an array of warriors in this class. The idea is to be able to call a method like this --> warrior.select(1) where it would get the warrior created at index 1. Hope that makes sense. Please explain to me why this error is happening.
Exception in thread "main" java.lang.StackOverflowError
at pking.Warrior.<init>(Warrior.java:42)
Code
package pking;
public class Warrior {
private String name;
private int age;
private String call;
private int attackPower;
private String weapon;
public Warrior(String myName, int myAge, String myCall, int myAttackPower) {
name = myName;
age = myAge;
call = myCall;
attackPower = myAttackPower;
weapon = "";
Warrior[] warriors = new Warrior[4];
warriors[0] = new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000);
warriors[1] = new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000);
warriors[2] = new Warrior("Gannicus", 30, "SLAYER", 8000);
warriors[3] = new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0);
}
//Prints warriors name
public void name() {
System.out.println(name);
}
//Prints warriors age
public void age() {
System.out.println(age);
}
//Prints warriors call
public void warriorsCall() {
System.out.println(call);
}
//Prints warriors attack power
public void attackPower() {
System.out.println(attackPower);
}
//Equips warriors weapon and prints message
public void equip(String myWeapon) {
weapon = myWeapon;
System.out.println("Equiped the: " + weapon);
}
//Prints warriors weapon
public void weapon() {
System.out.println(weapon);
}
}
Your constructor is running an infinite loop as you are calling it recursively. Check these lines in your constructor :
Warrior[] warriors = new Warrior[4];
warriors[0] = new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000);
warriors[1] = new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000);
warriors[2] = new Warrior("Gannicus", 30, "SLAYER OF VAGINAS", 8000);
warriors[3] = new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0);
for each Warrior array item instantiation you call the same constructor, which again try to create the array and initialise items, this keeps going on until stack overflows.
A better design strategy would be to create a new class, say Legion
, which will contain a collection of Warrior
s:
public class Legion {
Warrior[] warriors;
public Legion() {
warriors = new Warrior[4];
warriors[0] = new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000);
warriors[1] = new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000);
warriors[2] = new Warrior("Gannicus", 30, "SLAYER OF VAGINAS", 8000);
warriors[3] = new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0);
}
// getters and setters
}
I am not going to repeat the reason for the StackOverflow
exception, as that has already been answered (recursion problem in your constructor).
But, I think that what you probably wanted was to create those 4 special warriors once and not for every instance of a Warrior
. And maybe you wanted to have them as constants somehow.
So maybe something like this:
public class Warrior {
private static final List<Warrior> MEGA_WARRIORS =
Collections.unmodifiableList(Arrays.asList(
new Warrior("Spartacus", 40, "I AM SPARTACUS!", 9000),
new Warrior("Crixus", 35, "CHAMPION OF CAPUA", 8000),
new Warrior("Gannicus", 30, "SLAYER OF *censored*", 8000),
new Warrior("Alexander", 21, "I AM ALEXANDER, THE CODER", 0)
));
public static List<Warrior> getMegaWarriors() {
return MEGA_WARRIORS;
}
private String name;
private int age;
private String call;
private int attackPower;
private String weapon;
public Warrior(String myName, int myAge, String myCall, int myAttackPower) {
name = myName;
age = myAge;
call = myCall;
attackPower = myAttackPower;
weapon = "";
}
//Prints warriors name
public void name() {
System.out.println(name);
}
//Prints warriors age
public void age() {
System.out.println(age);
}
//Prints warriors call
public void warriorsCall() {
System.out.println(call);
}
//Prints warriors attack power
public void attackPower() {
System.out.println(attackPower);
}
//Equips warriors weapon and prints message
public void equip(String myWeapon) {
weapon = myWeapon;
System.out.println("Equiped the: " + weapon);
}
//Prints warriors weapon
public void weapon() {
System.out.println(weapon);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.