Why is the second instantiation of the Player object rewriting the first in this Java program?

I am attempting to write a program that will allow a user to input four characteristics of a “Player” object and add that object to an array list of other Player objects. However, it seems that all Player objects are treated as the same.

I suspect that all the program is doing is filling the arraylist with many references to the same object with the same instance variables, but I cannot figure out a way to fill the arraylist with discrete objects that all have different instance variables.

Driver Class Code

package playerdatamanager;

import java.util.*;

public class MainMenu {

    public static void main(String[] args) {
        
        ArrayList<Player> playerRoster = new ArrayList<>();

        Scanner input = new Scanner(System.in);

        int exitMenu = 0;

        while (!"7".equals(exitMenu)) {

            System.out.println("Please choose an option or press 7 to exit [1-7].");
            System.out.println("1. " + "Add a player");
            System.out.println("2. " + "Update a player");
            System.out.println("3. " + "Delete a player");
            System.out.println("4. " + "List all players");
            System.out.println("5. " + "Calculate the average BMIs for the players");
            System.out.println("6. " + "Display the details of the tallest player");
            System.out.println("7. " + "Exit");

            int menuOption = Integer.parseInt(input.nextLine());

            

            switch (menuOption) {
                case (1):

                    System.out.println("Please enter the name of the player.");

                    String newPlayerName = input.nextLine();

                    System.out.println("Please enter the jersey number of the player.");

                    int newPlayerJerseyNumber = Integer.parseInt(input.nextLine());

                    System.out.println("Please enter the height of the player.");

                    double newPlayerHeight = Double.parseDouble(input.nextLine());

                    System.out.println("Please enter the weight of the player.");

                    double newPlayerWeight = Double.parseDouble(input.nextLine());

                    playerRoster.add(new Player(newPlayerName, newPlayerJerseyNumber, newPlayerHeight, newPlayerWeight));

                    break;

                case (2):
                    System.out.println("Which player would you like to update?");

                    System.out.println(playerRoster.toString());

                    int playerIndex = Integer.parseInt(input.nextLine());

                    System.out.println("Selected" + playerRoster.get(playerIndex).toString());

                    System.out.println("Enter the value you would like to change[1. name ,2. jersey number,3. weight,4. height]");

                    int userValueUpdate = Integer.parseInt(input.nextLine());

                    switch (userValueUpdate) {

                        case (1):
                            System.out.println("Please enter the new value for the player's name.");

                            Player.setName(input.nextLine());

                            break;

                        case (2):
                            System.out.println("Please enter the new value for the player's jersey number.");

                            Player.setJerseyNumber(Integer.parseInt(input.nextLine()));

                            break;

                        case (3):

                            System.out.println("Please enter the new value for the player's weight.");

                            Player.setWeightInPounds(Double.parseDouble(input.nextLine()));

                            break;

                        case (4):
                            System.out.println("Please enter the new value for the player's height.");

                            Player.setHeightInInches(Double.parseDouble(input.nextLine()));

                            break;
                    }

                    break;
                case (3):

                    System.out.println("Which player would you like to delete?");

                    int userDeletion = Integer.parseInt(input.nextLine());

                    playerRoster.toString();

                    playerRoster.remove(userDeletion);

                    break;

                case (4):

                for (int i = 0; i < playerRoster.size();i++) {
                    
                    
                    
                    
                   
                    
                    
                    System.out.print("Name: " + playerRoster.get(i).getName() + "nHeight: " + Double.toString(playerRoster.get(i).getHeight()) + "nWeight: " + Double.toString(playerRoster.get(i).getWeight()) + "nJersey Number: " + Integer.toString( playerRoster.get(i).getJerseyNumber()) + "nn");
                }

                    break;



            }
        } 
    }
}

Player class code

package playerdatamanager;

/**
 *
 * @author Austin
 */
public class Player {

    private static String name;
    private static int jerseyNumber;
    private static double height;
    private static double weight;

    /**
     * The default constructor for the Player object.
     */
    public Player() {
    }

    /**
     * Overloaded constructor to initialize Player object with user values.
     *
     * @param name The name of the Player
     * @param jerseyNumber The Jersey Number of the Player
     * @param height The height of the Player
     * @param weight
     */
    public Player(String name, int jerseyNumber, double height, double weight) {
        this.name = name;
        this.jerseyNumber = jerseyNumber;
        this.height = height;
        this.weight = weight;
    }

    /**
     * method to calculate the BMI of a given player.
     *
     * @param height The height of a player.
     * @param weight The weight of a player.
     * @return the BMI of a player.
     */
    /**
     * setter method to set the player's name.
     *
     * @param userName The name given by the user.
     */
    public static void setName(String userName) {
        name = userName;
    }

    /**
     * setter method to set a player's jersey number.
     *
     * @param userJerseyNumber The jersey number given by the user.
     */
    public static void setJerseyNumber(int userJerseyNumber) {
        jerseyNumber = userJerseyNumber;
    }

    /**
     * setter method to set a player's height in inches.
     *
     * @param userHeight The height in inches given by the user.
     */
    public static void setHeightInInches(double userHeight) {
        height = userHeight;
    }

    /**
     * setter method to set a player's weight in pounds.
     *
     * @param userWeight The weight in pounds given by the user.
     */
    public static void setWeightInPounds(double userWeight) {
        weight = userWeight;
    }

    /**
     * getter method to retrieve the name of a player.
     *
     * @return The player's name.
     */
    public static String getName() {
        return name;
    }

    /**
     * getter method to retrieve the jersey number of a player.
     *
     * @return The player's jersey number.
     */
    public static int getJerseyNumber() {
        return jerseyNumber;
    }

    /**
     * getter method to retrieve the weight in pounds of a player.
     *
     * @return The player's weight in pounds.
     */
    public static double getWeight() {
        return weight;
    }

    /**
     * getter method to retrieve the height in inches of a player.
     *
     * @return The player's height in inches.
     */
    public static double getHeight() {
        return height;
    }

    public static double BMICalculation() {
        double BMI;

        BMI = 703 * (getWeight() / (getHeight() * getHeight()));

        return BMI;
    }

    public static void playerRosterToString(double height, double weight, int jerseyNumber, String name) {

        String playerStats = "Name: " + name + "nHeight: " + Double.toString(height) + "nWeight: " + Double.toString(weight) + "nJersey Number: " + Integer.toString(jerseyNumber);

        System.out.print(playerStats);

    }

}

The user should be able to input a player’s name, jersey number, weight, and height. Then the user should be able to read it anytime they like.

However, if a user inputs player name “Babe Ruth”, jersey number 1, weight 155, and height 70 that information will be stored. Then, if a user inputs player name “Joe Dimaggio”, jersey number 2, weight 170, and height 75 the program doesn’t store any information for Joe Dimaggio, but it will store Babe Ruth’s attributes.

If a user lists all players, the output is:

Name: Babe Ruth

Jersey number: 1

Weight: 155

Height: 70

Name: Babe Ruth

Jersey number: 1

Weight: 155

Height: 70

when it should be:

Name: Babe Ruth

Jersey number: 1

Weight: 155

Height: 70

Name: Joe Dimaggio

Jersey number: 2

Weight: 170

Height: 75

New contributor

LibreEstVitae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật