How to execute java function only once?

-2

Hi, I would like to know how in java I can execute a method only once (the first time I open the program), and then independently of all the times I decide to open it, I will not run again. I hope you understand. Thanks and regards.

    
asked by Luis Ramiro 06.06.2018 в 06:11
source

4 answers

0

If you only want the function to run only once when you open the program, the following occurs to me:

  • Create a Boolean static variable for example within the class in which the function is defined and initialized to true (for example).

  • Inside the function, put the code of this in an if, and the condition to enter the if is 'variable = true'.

  • Once inside the if, after executing the function code, set the Boolean variable to false, so that the successive times the function is called, when checking that the value is false, it will not enter in the if and it will not execute anything.

  • It would be something like this:

    
        Class A {
         private static boolean variableBooleana = true;
    
          public void tuFuncion(...) {
            if (variableBooleana == true) {
              
              variableBooleana = false;
            }
          }
        }
    
    

    I hope it serves you

        
    answered by 06.06.2018 в 08:46
    -1

    The way I think it would be more viable for you to do this would be for the function to verify the existence of a file and if it did not exist, execute the function and create it. This would mean that at the time of the first execution the code would be executed to collect and verify the information you require, which would then be stored in a file so that it can be used by the program from the second execution.

        
    answered by 06.06.2018 в 06:31
    -1

    I sense that you are going the same way:

    How to save a persistent variable in java even if you close the program

    Check that the file exists, if it exists, take the data, check and depending on that, execute your function as they told you above. If it does not exist, create the file ...

    To save a variable persistently use a file.

    To create files.

    link

    Summary algorithms I told you about:

    link

    Encryption:

    link

    You will find thousands of pages ... do not confuse the summary algorithms with the encryption, finally, put a code to help you, here nobody will do the work.

    Greetings.

        
    answered by 06.06.2018 в 08:36
    -1

    I also had the same problem not long ago but the best thing to save a variable is creating a file and verifying it every time you start the program.

    To write to the file:

    void escribir(variable){
            FileWriter fw = new FileWriter("nombrearchivo.txt");
            fw.write(variable)
    
            fw.close(); //Cerramos el FileWriter para que se guarden los cambios
                    }
    

    // To read the file

            String leer(){
    
            // Abres el archivo
            BufferedReader archivo = new BufferedReader(new FileReader("nombrearchivo.txt"));
    
            return archivo.readLine() // Devuelves el valor que se guarda en el archivo
    
    
    archivo.close();
        }
    
        
    answered by 06.06.2018 в 12:27