knowing that Java is different from PHP, is there a way to reconstruct such a behaviour in Java?:
In PHP I can make something like this:
function a_function() {
static $var = "something";
// do something...
}
After I call this function the first time, all subsequent calls to this function would result in a $var not being reset to "something" at all (its value will be preserved across multiple calls to this function, i.e. if the value of $var will be changed while // do something... is executed, then after a subsequent call, the value of $var would be the last assigned value to $var).
I was wondering if there's a same/like method to do this in Java (declaring a static local variable in a method in Java is not possible, so I guess I can achieve this in some other ways). I thought of making a protected or private static field and use it across multiple calls to a method (here a possible way with a boolean):
public class A {
protected static boolean isSomething = "false";
public void aMethod() {
// do something... e.g. change the isSomething boolean
// if some condition occurs
}
}
What do you think about this approach and what would you do in such a case? Are there other ways to accomplish this?
Thanks for the attention!