Trying to create two instances of the same object type but end up with two variables referencing the same object

Ciaran Muir :

I've been trying to create a tic-tac-toe game in Java to help me improve at creating Object Oriented projects from scratch. But I have ran into a problem and am looking for some guidance.

I'm only half way through the project at the moment so I'll just give you a quick update on what I've done so far.

The project currently has a Game class as the driver. It has a Board class which stores a map between the locations on the board (A1, A2, A3...) and their values ('X', 'O', null). And a Player class which holds fields for the team the player is on (Xs or Os) and the number of wins (functionality for winning has not been implemented yet).

The issue I'm having is with creating two instances of the Player class (player1 and player2) from Game.

I've defined them as fields in Game like so:

public static Player player1 = new Player();
public static Player player2 = new Player();

player1 is then asked to choose between Xs and Os and player2 is given the opposite.

player1.setTeam(Character.toUpperCase(teamChoice));

if(player1.getTeam() == 'X')
     player2.setTeam('O');
else
     player2.setTeam('X');

In my head this should work. However, when I print out the player choices it prints out whatever player2 has been set to be.

System.out.println("Player 1 you have selected " + player1.getTeam() + "\n");
System.out.println("Player 2 you will be " + player2.getTeam() + "\n");

The contents of the Player class are seen below:

public class Player {
    public static int wins = 0;
    public static Character team;

    public Player(){
    }

    public int getWins(){return wins;}

    public Character getTeam(){return team;}

    public void setTeam(Character team){
        Player.team = team;
    }

}
ernest_k :

The problem is here:

public static Character team;

team is static, and that means the variable team does not belong to any given instance. It belongs to the class.

As things are, whenever you call player1.setTeam or player2.setTeam, it's the class-wide team variable that is being changed, not something specific to player1 or to player2.

You need to make team instance-specific. All it takes is to remove the static keyword.

private char team;

You'll also notice that I made it private, as that's the recommended practice (you've already defined a getter and a setter for it, so it needs not be public).
The other change there is from Character to char. You're making comparisons using == somewhere in your code (if(player1.getTeam() == 'X')), using a reference type may lead to unexpected results, using the primitive type here is the simplest change you need to make.

Your class will end up looking like

public class Player {
    private int wins = 0;
    private char team;

    public Player() {
    }

    public int getWins() {
        return wins;
    }

    public char getTeam() {
        return team;
    }

    public void setTeam(char team) {
        this.team = team;
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=396634&siteId=1