I have a question about boolean values in Java. Let\'s say I have a program like this:
boolean test = false;
...
foo(test)
foo2(test)
foo(Boolean test){
t
You need to be aware that:
Because of 1 and 2, you have no way to change the state of the Boolean pass in the method.
You mostly have 2 choice:
Choice 1: Have a mutable holder for boolean like:
class BooleanHolder {
public boolean value; // better make getter/setter/ctor for this, just to demonstrate
}
so in your code it should look like:
void foo(BooleanHolder test) {
test.value=true;
}
Choice 2: A more reasonable choice: return the value from your method:
boolean foo(boolean test) {
return true; // or you may do something else base on test or other states
}
the caller should use it like:
boolean value= false;
value = foo(value);
foo2(value);
This approach is preferrable as it fit better with normal Java coding practices, and by the method signature it gives hint to the caller that it is going to return you a new value base on your input