0

I have made a program that starts with a log in form. I want to put the option to change the password and the username, but after closing the program and when I open it again these variables get the default value. This is the code of changing class where username is a global variable named "emriuser" and password is "fkalimi" , I made them global to use in different classes on my project and I don't know, it is the right way? Therefore I want to know how to store their value after closing the program:

    public class Change extends javax.swing.JFrame {
    // .......

   private void oActionPerformed(java.awt.event.ActionEvent evt) {  
   String p = pass2.getText();
   String u = uiri.getText();

   if (u.equals(emriuser) && (p.equals(fkalimi))){ 
       emriuser=u;
       fkalimi=p;
       //  here a code how to store the values
   }

Also I want to know how to call their value when I open next time the program:

 public class LogIn extends javax.swing.JFrame {
   // .......
    /**
 *  here the code I dont know
     .........

     and then is the code I wrote to be executed if the pass and username haven't change
 */
    emriuser="user1";
    fkalimi="DThShI=0I";

    String p1 = pass1.getText();
       String u1 = uname1.getText();

    if (u1.equals(emriuser) && (p1.equals(fkalimi))){ 



  setVisible(false);
  f1 a=new f1 ();
        a.setTitle("Main Menu");
        a.pack();
        a.show();
                    }

Can Someone help me please, thanks in advance!

ArliE
  • 1
  • 1
  • 3

3 Answers3

2

There is no direct way to get the value of the variables, because when closing the application the Java-Runtime is closed and all variables are removed from the memory by the garbage collector.

To save the values you have to write them into a file.
Either you serialize the whole object or you save it as text/csv/json.
When loading the application you can load these values again.

Serialize object

Write text to File

tgallei
  • 827
  • 3
  • 13
  • 22
  • Or [the preferences API](https://docs.oracle.com/javase/8/docs/api/java/util/prefs/Preferences.html#put-java.lang.String-java.lang.String-), which might be more convenient for holding a string across multiple runs. – Holger Jan 13 '20 at 13:25
2

I want to know how to store their value after closing the program

There is no single best way to do this, but there are many options. In general, any value X that you want to retrieve later requires that you store X somewhere outside of your code running in the JVM.

Here are some possible general ways to store a value (there are others, too):

  • a database – write a value to database, then query database later. Note that the database could be present on the same machine as your JVM, or it could be some altogether different host. Specifics options include MySQL, Postgres, Redis, Cassandra, etc.
  • a filesystem – write a value to a file, read the file later. Note that the file could be present on the same machine, or on a remote filesystem (using something like NFS or NTFS).
  • an API endpoint – your code makes an HTTP call to an API to save X, then later make an API call to retrieve X. Note this requires someone to build whatever API, and deal with storage. It's possible that the API has no persistent storage of its own, instead serving answers from running memory; or the API service could itself use some other data store to read/write answers.
  • a library – your codes calls library code to save X, and then the library under the covers does something to persist X somewhere.
Kaan
  • 5,434
  • 3
  • 19
  • 41
0

Making variables global will not persist them across program executions; it only makes them visible to a bigger scope.

If you need to keep information from one execution to the other, you need to save the information to the hard drive, be it by writing it directly to a file or to a database. This way, every time your program runs, it should check this storage and retrieve the data.

Check Hibernate, a library to map Java objects to a database.

miguescri
  • 71
  • 3