I have a scenario where I want to set a Boolean
object and then use its booleanValue()
in a constructor later on in the method. However, the scope in which the object is set is different. It is set in a method called by the method in which the object is first instantiated. Based on my understanding of how Java passes primitive and object arguments and reading several posts online (such as this), when an object is passed into a method, its properties are passed by reference and any change in the called method should be reflected in the calling method after the called method has finished execution. However I am noticing that when the called method is finished, any change in there is not taking effect in the calling method.
Here is a snapshot of my scenario:
private CustomObject1 callingMethod(){
Boolean b = Boolean.TRUE;
List<CustomObject2> list = this.calledMethod(b);
//Create CustomObject1 with b.booleanValue() as one of the arguments in the constructor
}
private List<CustomObject2> calledMethod(Boolean b){
...
...
if(condition){
b = Boolean.FALSE;
}
...
...
}
By the time the code reaches the CustomObject
creation b.booleanValue()
is always true, even if the if-statement in callingMethod()
is true and the Boolean
is set to false in that method.
I am reluctant to change the return type of the calling method to be a boolean
as it would require changes in other bits of code that may call this method. Currently they only need a signature change but a return type change would require more work as the logic needs to be maintained (i.e populating a list and then doing something with it)
First, there is a lot of misinformation about parameter passing in Java, like "objects are passed by reference, primitives are passed by value" which is not true. Everything is passed by value.
You're not passing an object by reference in Java, you're passing an object reference by value. Boolean b
does not hold a Boolean
object, it holds a reference (a pointer) to a Boolean
object.
Here's a good article about it: http://javadude.com/articles/passbyvalue.htm
Second, Boolean
(like the other wrapper objects and also String
) are immutable objects. So with an immutable object, and as they are passed by value (better said, their references are passed by value), you cannot achieve what you desire. You'll need to have a mutable object instead, like @rob mentioned.
Or use MutableBoolean
from Apache Commons Lang.