I have the following code:
public static void main(String[] args) {
Player players[] = new Player[2];
Scanner kb = new Scanner(System.in);
System.out.p
Name field should not be static. Static means that the variable is actually global and shared across all class instances.
Its because of the static field. Statics are used across object instances. They are stored at class level.
Below code would work:
class Player
{
String name;
public Player(String playerName)
{
name = playerName;
}
public String getName()
{
return name;
}
}
With the keyword static you have made name
a class variable which is NOT an instance variable. A class variable is common to all the objects. Click for some more reading.
Change static string name to private string name