我遇到了一个这样设置的类:
public class MyClass {
private static boolean started = false;
private MyClass(){
}
public static void doSomething(){
if(started){
return;
}
started = true;
//code below that is only supposed to run
//run if not started
}
}
我对静态方法的理解是,除非它们是常量并且不会更改,否则不应在其中使用类变量。相反,您应该使用参数。我的问题是为什么通过执行 MyClass.doSomething() 多次调用时这不会中断。在我看来,它不应该起作用,但确实起作用。它只会通过 if 语句一次。
那么有人可以向我解释为什么这不会中断吗?