简体   繁体   English

如何在关闭应用程序后使变量存储值?

[英]How to make a variable store a value even after closing of application?

well i have this class Launcher.java : 好吧,我有这个类Launcher.java:

public class Launcher {

public static void main(String... args){

    new App();
    System.out.println( "Innovate's current build is : " + App.getBuild());
}


}

and the class App.java : 和类App.java:

import javax.swing.*;

public class App extends JFrame {

static int build;

public App(){

    super("Innovate");
    setBuild(getBuild() + 1);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500,200);
    setResizable(false);

}

public static void setBuild(int inc){
    build = inc;
}

public static int getBuild(){
    return build;
}

}

I am trying to make it every time the application is run the build number increments by 1, however whenever i run it prints out 1 and does not increment, how will i go about doing this? 我试图在每次运行应用程序时使它的内部版本号递增1,但是每当我运行它时,它都会打印1而不递增,那么我该怎么做呢?

thanks 谢谢

You could go ahead and serialize the build number. 您可以继续并序列化内部版本号。 because it will be lost every time the application is shut down. 因为它会在每次关闭应用程序时丢失。

Here is a tutorial on how to serialize objects 这是有关如何序列化对象的教程

static int build;

You have to save the build number into file when your are exiting from your app. 从应用程序退出时,必须将内部版本号保存到文件中。 While creating your first App class object you have read from file and save it into your build variable. 在创建第一个App类对象时,您已从文件中读取并将其保存到构建变量中。 you can use Static Initializer Block for reading data from file because static block is executed only once: the first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of that class). 您可以使用静态初始化程序块从文件中读取数据,因为静态块仅执行一次:第一次创建该类的对象或第一次访问该类的静态成员(即使您从未创建该对象的对象)该课)。

As others have mentioned, you can use serialization to persist the state of the object, or you could read / write the value to something like a properties file. 正如其他人提到的那样,您可以使用序列化来保留对象的状态,也可以将值读/写到诸如属性文件之类的东西。 While these will work I'm not sure its the best solution for what you are trying to to. 尽管这些方法会起作用,但我不确定它是否是您要尝试的最佳解决方案。 If you are trying to keep track of a build number for your application, typically that would be kept as a static value from within your project and only change every time you truly deploy your app, whether it be a release/snapshot/etc. 如果您尝试跟踪应用程序的内部版本号,通常会在项目中将其作为静态值保留,并且仅在每次真正部署应用程序时才更改,无论是发行版/快照/等等。 Hope that helps. 希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在java中关闭程序后如何存储变量的值 - How to store the value of a variable after closing the program in java JFrame 即使在关闭后仍保持应用程序运行 - JFrame keeps application running even after closing 即使关闭应用程序后,我也可以在一定时间间隔下运行后台服务吗 - Can I Make A Background Service That runs at certain intervals of time even after closing the Application 在以as-> Java应用程序运行后,如何保持变量值? - How to keep a variable value even after doing run as-> Java Application? 关闭程序后如何重用先前的变量值 - how to reuse previous variable value after closing program 即使关闭了应用程序,程序仍在Netbeans中运行 - Program is still running in Netbeans, even after closing the Application 关闭另一个应用程序后如何恢复数据 - How to recover data after closing another application 更改后如何使计数器变量保持其值? - How to make the counter variable retain its value after changing it? 即使应用程序被强制停止,也要重新启动服务,即使关闭应用程序,也要在后台继续运行服务如何? - Restart the service even if app is force-stopped and Keep running service in background even after closing the app How? 即使应用程序被强制停止,也要重新启动服务;关闭应用程序后,仍要在后台运行服务。 - Restart the service even if app is force-stopped and Keep running service in background even after closing the app How?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM