I came across a class that was configured as follows:
public class MyClass {
private static boolean started = false;
private MyClass(){
}
public static void doSomething(){
if(started){
return;
}
started = true;
}
}
My understanding of static methods is that you should not use class variables in them unless they are constant and change. You should use options instead. My question is: why is this not broken when called multiple times by executing MyClass.doSomething (). It seems to me that this should not work, but it does. It will only pass after the if statement is executed.
So can someone explain to me why this won't break?
source
share