Home How do you pass an updated global class variable from one method into another?
Reply: 0

How do you pass an updated global class variable from one method into another?

user1074
1#
user1074 Published in July 18, 2018, 10:20 pm

I update a variable (which is global in the class) in one method and I cannot seem to be able to then pass that updated variable into another method. Any help would be appreciated, thank you. Here's my shortened code:

public class Game{

    private int randomIndexX;

    protected String spawn(){
        randomIndexX = randomGenerator.nextInt(10);
        return null;
    }

    protected String test(){
        System.out.println(this.randomIndexX);
        return null;
    }
}


public class Player extends Game{
    protected String getNextAction(String command) {
        switch(command){
            case "test":
                test();
                break;
        }
    }
}


public static void main(String[] args) {
    Game game = new Game();
    Player player = new Player();

    game.spawn();
    player.getInputFromConsole();
}

EDIT: so when i call test() from the Player class i want it to print out randomIndexX but it still doesn't seem to be working even with this.randomIndexX in the method test()

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309719 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO