Search code examples
javaswingarraylistjframeglobal-variables

empty ArrayList after closeing JFrame


I have different classes, but just one public ArrayList, which is accessible from every class. My first class is called User and looks like this

public class User{

    private String fName, lName, username, password;
  
    public ArrayList<Student> students = new ArrayList<>();

    public User(String username, String fName, String lName, String password){
        
        this.username = username;
        this. fName = fName;
        this.lName = lName;
        this.password = password
    }

//Getter and Setter Methods here

//here my own methods
    public void addStudent(Student student){
        students.add(student);
    }

}

My second class is called Student and looks like this

public class Student extends User{

    private String username;
    private String fName;
    private String lName;
    private String password;
    private Course course;

    public Student(String username, String f, String l, String pw, Course course){
    
        super(username, f, l, pw);
        this.course = course;
    }

    public Student(){
    super(String username, String f, String l, String pw);
    }

    //Getter and Setter Methods
}

Now my trouble begins, in my 3rd class, called Window I am able to add a new student to my ArrayList. For debuging I printed the size of that list in my console and it was bigger than before, so I know for sure, that a new Student is inside my array list.

This window is something like a registration process. After the successful registration the user can close the window. If he tries to login in right after the registration it doesn't work, because my arraylist is empty again after I close my (registration) window:

public class Window{

Student student 0 = new Student();

private void saveButtonActionPerformed(ActionEvent evt) {
            
                String first;
                String last;
                String pw;
                String username;
                Course kurse1;

                if(condition){

                    //get userinputs
                    first = fName.getText();
                    last = lName.getText();
                    pw = password.getText();
                    username = first.toLowerCase() + "." + last.toLowerCase();
                    kurse1 = new Course(kurseN1.getText());

                    //add Student to list
                    Student student = new Studenten(username, first, last, pw, new Kurse(kurseN1.getText()));
                    student0.studentens.add(student);

                    System.out.println("Number of students: " + student0.students.size());
                
                    
                }else{
                    JOptionPane.showMessageDialog(null, "please fill out all required fields");
                }
}

//with this method I open the login window
private void btnClick(ActionEvent e){

    LoginWindow login = new LoginWindow();
    login.setVisible(true);
}

Solution

  • You're confusing public and static. The arrayList is a (non-static) member of User, so a new ArrayList is created for every User you create.

    If it were a static variable, that means that only a single list gets created in the JVM and all instances of User share that one list. I think that is what you intended.

    Public only refers to whether or not code that belongs to other classes can see it and use it directly, and has no effect on how, when, or how often it is created.

    Side note: ArrayList is not thread-safe. So if you are accessing it by two different threads, such as you'd often get in a web application or even a modern UI, you might get some unexpected conflicts. If two are trying to write to it, one write might be lost or the whole thing could get corrupted.