static long sum(int n, long total) {
"static" boolean exitConditionMet = false;
if(n == 1) {
exitConditionMet = true;
return 1;
} else {
return total + sum(n-1, total);
}
}
I need to execute different code if this variable is true. And its address should not change for all recursive calls. My code is different but I provided this as an example, please ignore the errors, it is just for understanding my problem.
You can suggest using a parameter, but I don't want that option.
Is it possible to achieve this using Java? I hope I explained it clearly.
I tried chatgpt and another answering platform. im expecting an equivalent keyword
There is no Java keyword that will allow you to declare a local variable as static. The closest in Java to that C++ would be:
class Test {
static boolean exitConditionMet = false;
static long sum(int n, long total) {
if (n == 1) {
exitConditionMet = true;
return 1;
} else {
return total + sum(n-1, total);
}
}
}