(Java RPG) object construction inside an if statement

Viewed 70

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();
                }
            }
        }
    }   
}
3 Answers

The scope of those variables is limited to inside the if/else blocks. If you want to access it outside, you should do the following:

Character player = null;

if (a2==1){
    player = new Warrior();
} else if (a2==2){
    player = new Rouge();
} else if (a2==3){
    player = new Mage();
} else{
    player = new Fool();
}

// by now, player was instantiated by some concrete class and you can use it

If each Character implements their own fightEnemy() method, then you can leverage polymorphism here to automatically choose the correct concrete method:

 ...
 if (choice==1){
        // java will figure out which "Character" this really is
        // then it will call the fightEnemy method for that specific "Character" type
        player.fightEnemy();
 }
 ...

Change

if (a2==1){
    Warrior wplayer = new Warrior();
}

to

if (a2==1){
       wplayer = new Warrior();
}

You have several mistakes in this code.

  1. Non-static variable visibility is limited to {}. So you can see, that whe you write if(a2 == 1) { Warrior wplayer = new Warrior(); } then wplayer variable is visible only within {} and cannot be accessed after if...else. To fix it, you should define Warrior wplayer outside if...else and initialize inside if...else:

    Warrior wplayer = null;

    if(a2 == 1) { Warrior wplayer = new Warrior(); }

    // wplayer is visible here

  2. Additionnaly, you should look at structure of your code. You have several types of Player. All players have specific fightEnemy() implementation and to choose correct instance you use if...else. Imagine, if later you add some more type of Player, then you have to modify all these if...else. This is not good. Let me give you example how it could be done.

Example

interface Player {
    void fightEnemy();
}

class Warrior implements Player {
    public void fightEnemy() { }
}

class Rouge implements Player {
    public void fightEnemy() { }
}

class Mage implements Player {
    public void fightEnemy() { }
}

class Fool implements Player {
    public void fightEnemy() { }
}

Scanner scan = new Scanner(System.in);
int a2 = scan.nextInt();

Player player;

if (a2 == 1) {
    player = new Warrior();
} else if (a2 == 2) {
    player = new Rouge();
{ else if (a2 == 3) {
    player = new Mage();
} else {
    player = new Fool();
}

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) {
    player.fightEnemy();
}
Related