tgodfrey0
tgodfrey0

Reputation: 5

Java - List of Objects Changes Previous Elements

I am trying to create a list of objects from a database but whenever I add another element to the end of the list it changes the values of the previous elements. I have seen similar questions on here but none seem to help my problem.

Here is the code for the class Deck

public static class Deck {
    private static int deckid;
    private static String deckName;
    private static int deckCreatorID;
    private static boolean isDeckPublic;
    private static String deckDescription;

    public Deck(int id, String name, int creator, boolean isPublic, String description){
        deckid = id;
        deckName = name;
        deckCreatorID = creator;
        isDeckPublic = isPublic;
        deckDescription = description;
    }

    public String getDeckName(){
        return deckName;
    }

    public String getDeckDescription(){
        return deckDescription;
    }

    public int getDeckCreator(){
        return deckCreatorID;
    }

    public int getDeckid() { return deckid; }

    public boolean getDeckPublic() { return isDeckPublic; }

}

Here is the code to create and add the objects to the list:

public static List<marketplaceController.Deck> getAllCards(){
    List<marketplaceController.Deck> deckList = new ArrayList<>();

    List<Integer> idList = new ArrayList<>();
    List<String> nameList = new ArrayList<>();
    List<Integer> cIdList = new ArrayList<>();
    List<Boolean> publicList = new ArrayList<>();
    List<String> descList = new ArrayList<>();

    try{ // This try-catch block is mainly from https://www.javatpoint.com/example-to-connect-to-the-mysql-database
        Class.forName("com.mysql.cj.jdbc.Driver"); // Define the driver to use
        Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/users","root","UnlockDB.123"); // Connect to the local db
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery("select * from decks"); // Execute the query on the db

        while(rs.next()) { // Runs while columns are to be store
            idList.add(rs.getInt(1));
            nameList.add(rs.getString(2));
            cIdList.add(rs.getInt(3));
            publicList.add(rs.getBoolean(4));
            descList.add(rs.getString(5));
        }

        con.close();
    }
    catch(Exception e){
        loginController.popup("An error occurred connecting to the database", "An error occurred");
    }

    for(int i = 0; i < idList.size(); ++i){ // This loop outputs the correct data
        deckList.add(new marketplaceController.Deck(idList.get(i), nameList.get(i), cIdList.get(i), publicList.get(i), descList.get(i)));
        System.out.println(deckList.get(i).getDeckid());
        System.out.println(deckList.get(i).getDeckName());
        System.out.println(deckList.get(i).getDeckCreator());
        System.out.println(deckList.get(i).getDeckPublic());
        System.out.println(deckList.get(i).getDeckDescription());
    }

    for(int i = 0; i < deckList.size(); ++i){ // This outputs the overwitten data
        System.out.println(deckList.get(i).getDeckid());
        System.out.println(deckList.get(i).getDeckName());
        System.out.println(deckList.get(i).getDeckCreator());
        System.out.println(deckList.get(i).getDeckPublic());
        System.out.println(deckList.get(i).getDeckDescription());
    }


    return deckList;
}

When the elements of deckList are printed straight after they are created they show the correct elements e.g.

1
TESTDECK
1
true
""

2
TESTDECK2
1
true
""

However, when I iterate through the completed list and print the contents the following is printed:

2
TESTDECK2
1
true
""

2
TESTDECK2
1
true
""

This is probably a very stupiud question but I'm reltively new to Java and so any help is greatly appreciated

Upvotes: 0

Views: 51

Answers (1)

Christoph Grimmer
Christoph Grimmer

Reputation: 4313

As @jhamon pointed out using static variables can be a very bad idea when you don't know what this means. Simply put a static field is shared among all instances of a class because it is a property of the class and not the instance. So when you have 10 instances of Deck all of them will return the same value for e.g. deckid.

Upvotes: 1

Related Questions