简体   繁体   中英

How to change the value of a boolean var inside a method?

This is maybe so silly. I have a boolean variable inside the main method. By calling another method of this class or another class I want my boolean value to be modified in the main method. I do this but the change happens only in the called method(locally), not the caller(main). I think this is because of the pass-by-value feature of java. I even tried Boolean type, but the same problem there!

Actually I'll use this to manage the ordering of concurrent threads. The main processor will check for the boolean value of every thread to see if it is ok to continue and tick the clock. After ticking the clock the main will make the vars false and will wait until the vars are again true. the sub-threads will start their task if the boolean value of them each is false. After the task is done they will make the vars to true so the main processor is able to tick again. So I want something without a return. I mean as the value is changed inside the method the main could see it.

boolean var = true;
var = methodWhichReturnsTheNewValueOfTheVariable();

and inside the called method:

return newBooleanValue;

or

SomeObjectWithBooleanVariableInside var = new SomeObjectWithBooleanVariableInside(true);
methodWhichModifiesTheObject(var);

and inside the called method:

var.setBooleanValue(newBooleanValue);

A Boolean is such an object: it contains a boolean value. But it's intentionally designed as immutable: its wrapped boolean value can't be changed. So you need to create your own, functional object.

The usual way to do this is the following:

public static void main(String[] args) {
    boolean myVar = true;

    ...
    ...

    myVar = myFunction();
}

public static boolean myFunction() {
    // the method should do it's calculation and return the value:
    return false;
}

Yes - you cannot modify passed-by-value parameter inside a method in Java (for example in C# you would write method(ref param) ).

Why can't you return this value using the method:

public boolean method(params...) {...}

Or you can pass in param the reference to caller:

public void method(params..., CallerClass caller) { 
    //do something
    caller.setValue(Boolean.FALSE);
}

Or you can make this variable accessible in caller and calling method scopes - static variable, etc.

Primitive types are passed by value, so you can't change variables coming as parameter in a method.

This makes also easier to understand how a program works, since this kind of behavior is made more evident in an invocation like this:

boolean prime = false;
prime = isPrime(number);

you can see that found variable is reassigned; while you can assume that number will remain unchanged. This helps in code readability.

There is a dirty trick that sometime can be used. Since arrays are objects, you can use an array to wrap a primitive type:

boolean[] prime = { false };
isPrime(number, result);

public void isPrime(int number, boolean[] result) {
    result[0] = true;
}

The object reference is passed by value too, but here we change the content of the array, not the array reference.

This works. But, I don't recommend to design your code like this. Sometimes, knowing this trick can be useful in unit tests though.

when you think that you changed the value of the primitive boolean it only changed the value in the scope of that method. same with Boolean as it is immutable. changing its value actually assigned a new value to it inside the scope of that method. you should return the new value from that method and then assign it or you could also use a global boolean that is known to all and to change that one.

(and by the way, if you're dealing with concurrency check out AtomicBoolean)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM