Say you have a class called team:
public class Team {
public void Team() {
Player player1 = new Player();
Player player2 = new Player();
if (player1.getName == "") {
player1.setName = console.next();
}
else if (player2.getName == "") {
player2.setName = console.next();
}
}
}
And another class called player:
public class Player {
private String name;
public void setName(String inputName) {
name = inputName;
}
public String getName() {
return name;
}
}
If I wanted to set a name for player1, I could do so by calling the Team() method. But if I then want to set a name for player2, it's going to reset the name of player 1 as it is making a fresh player1 and player2 object each time the method is called, right?
So my question is, if I want to only initialize the player1 and player2 objects the first time I run the Team() method, how would I achieve that? Because if I moved the object initializations out of the Team class, I wouldn't have access to getName and setName.
I'm quite new to this so any help such as an alternate way of achieving this (without the use of arrays) would be greatly appreciated! Thanks.