我想创建一个布尔变量,它只是告诉用户这是第一次运行类,还是第二次(或nth之后的时间).我该怎么做?
public class boolTest {
static boolean dirty;
public static void main (String[] args) {
dirty = false;
if (!dirty) {
System.out.println("First time running.");
dirty = true;
System.out.println("Now it's true.");
}
else
{
System.out.println("this is already true.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,它总是说"第一次运行" -
C:\ Java_Scratch> java boolTest第一次运行.现在这是真的.
C:\ Java_Scratch> java boolTest第一次运行.现在这是真的.
在整个程序的运行时,类的所有实例共享一个类中的静态字段.一旦程序停止并重新执行,堆上存储的所有内容都将消失,无论它是否为静态.
要做你想做的事,你需要将它存储到一个文件,或者一个环境变量或一些外部存储器,它不能在程序的内存中
| 归档时间: |
|
| 查看次数: |
206 次 |
| 最近记录: |