Home > Software engineering >  Java static variable issue
Java static variable issue

Time:08-14

I'm experiencing issue with couple static variables.

I have a datastore class named LoginDataStore with the following codes:

class LoginDataStore {
    private static String userEmail;
    private static String userPass;
    
    public LoginDataStore(String userEmail, String userPassword) {
        LoginDataStore.setUserEmail(userEmail);
        LoginDataStore.setUserPassword(userPassword);
    }
    
    public static void setUserEmail(String email) {
        LoginDataStore.userEmail = email;
    }
    
    public static String getUserEmail() {
        return LoginDataStore.userEmail;
    }
    
    public static void setUserPassword(String password) {
        LoginDataStore.userPass = password;
    }
    
    public static String getUserPassword() {
        return LoginDataStore.userPass;
    }
}

and the main thread will set those 2 variables when the user press "login" button, using this function:

            if (loginThreadIsRunning) {
                loginThreadIsRunning = false;
            }
LoginDataStore loginDataStore = new LoginDataStore(emailInput.getText(), String.valueOf(passwordInput.getPassword()));

Then i initialize the thread:

        LoginThread lt = new LoginThread();
        Thread t1 = new Thread(lt);
        t1.start();

the thread has this codes:

class LoginThread extends LoginUI implements Runnable{  
    public void run() {
        LoginUI loginUI = new LoginUI();
        if (!loginUI.loginThreadIsRunning) {
            loginUI.loginThreadIsRunning = true;
            System.out.println("[LoginThread] has started.");
            String loginEmail = LoginDataStore.getUserEmail();
            String loginPass = LoginDataStore.getUserPassword();
            System.out.println("[LoginThread] loginEmail: "   loginEmail   " | loginPass: "   loginPass);
            loginUI.loginThreadIsDone = true;
        }
    }
}

It's currently just for debugging before i implement the backend server.

However, when i try to login the first time, the Thread outputs the data correctly, but if i try to login again, the output is simply empty, same is for if i don't change the data but just repress the login button, but also if i do change the details in the login UI.

Example output:

[LoginThread] has started.
[LoginThread] loginEmail: test | loginPass: null
[LoginThread] has started.
[LoginThread] loginEmail:  | loginPass: null

Does anyone know what may cause this issue?

CodePudding user response:

In your code, you are not storing the 'userPassword'. You are overring the password with the email.

public LoginDataStore(String userEmail, String userPassword) {
        LoginDataStore.setUserEmail(userEmail);
        LoginDataStore.setUserEmail(userPassword);  //<- Here
    }

Implement function to setPassword and modify the constructor.

    public LoginDataStore(String userEmail, String userPassword) {
        LoginDataStore.setUserEmail(userEmail);
        LoginDataStore.setUserPassword(userPassword);
    }
    
    public static void setUserPassword(String userPassword) {
        LoginDataStore.userPass = userPassword;
    }

CodePudding user response:

Try removing the static modifier from your instance variables inside your LoginDataStore class. A static variable belongs to the class and not a particular instance.

Leaving the instance variables as non-static variables will allow you to create multiple instances of LoginDataStore with unique or same values for the userEmail and userPass

More on this here https://www.baeldung.com/java-static-variables-initialization#:~:text=In Java, static variables are,(non-static variables).

  • Related