I've made 4 classes (Mage,Rouge,Warrior&fool) that extend a class 'Character'. Each of these have their own specific battle methods inside their respective classes. However since only one of these objects are being constructed inside an if/else statement java doesn't recognize them as existing. The problem is I need to call the class specific method outside of the if/else statement. I tried initializing the 4 objects as null first then constructing/overwriting them but when I call the method it still refers to them as null. Formatting is a little screwed up but here:
EDIT: Thanks for the feedback!
public class Rpg {
public static Warrior wplayer = null;
public static Rouge rplayer = null;
public static Mage mplayer = null;
public static Fool fplayer = null;
public static void main(String[] args){
a2 = scan.nextInt();
if (a2 == 1){
Warrior wplayer = new Warrior();
} else if (a2 == 2) {
Rouge rplayer = new Rouge();
} else if (a2 == 3) {
Mage mplayer = new Mage();
} else {
Fool fplayer = new Fool();
while (!notdone){
System.out.println("1: Arena");
System.out.println("2: Blacksmith");
System.out.println("3: Shop");
System.out.println("4: Leave town");
System.out.println("5: Save and Quit");
int choice = scan.nextInt();
if (choice == 1 && wplayer != null){
wplayer.fightEnemy();
} else if (choice == 1 && rplayer != null){
rplayer.fightEnemy();
} else if (choice == 1 && mplayer != null){
mplayer.fightEnemy();
} else {
fplayer.fightEnemy();
}
}
}
}
}