¿Cómo cambiar el valor de una var booleana dentro de un método?

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.

preguntado el 23 de diciembre de 12 a las 15:12

5 Respuestas

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.

Respondido el 23 de diciembre de 12 a las 15:12

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;
}

Respondido el 23 de diciembre de 12 a las 15:12

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.

Respondido el 23 de diciembre de 12 a las 15:12

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);

puedes ver eso 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.

Respondido el 23 de diciembre de 12 a las 15:12

Everything in Java is passed by value. Not just primitive types. - Rohit jainista

Yes, true. But for objects, the reference is passed by value; which means you can change the object referenced (unless it is immutable). BTW, yes, technically you are perfectly right, but I didn't say otherwise. - Luigi R. Viggiano

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)

Respondido el 23 de diciembre de 12 a las 17:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.