0

I have a static function to save the high score! I wonder is there a easy way to call this function automaticlly when we exit a program. If in C++, I can put this function in destructor and it works. But in java I dont know how! Any idea to do this? Thank you!

2 Answers2

2

For all kind of java applications, you could REGISTER A SHUTDOWN HOOK, like this:

   Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    System.out.println("Your job when app shutdown");
   }
  });
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
0

for Swing GUI programs

myform.addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowClosing(java.awt.event.WindowEvent e) {
                    // your exit codes here
                    System.exit(0);
            }
        });
Soley
  • 1,716
  • 1
  • 19
  • 33