I have tried to change the value of the variable x below by using a getter and setter.
package game;
public class Game {
private int x;
private int y;
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public static void main(String[] args) {
Game game = new Game();
Command command = new Command();
command.changeX();
System.out.println(game.getX());
}
}
I have another class that has a method that changes the integer x, by using the get and set method.
package game;
public class Command {
Game game = new Game();
public void changeX() {
int x = game.getX();
game.setX(x + 1);
}
}
When I run the program the console prints out 0, but it is supposed to be one. Then, if I try to print out the value of x using the getX method in Command right after I set the variable to one, it prints out 1. I am trying to see if there is a way I can do this without using static variables.