Your BooleanTest2 is assigning a new reference (i.e., a reference to a
different object) to the local variable "b". It's not changing the
object whose reference was passed in from main().
If you wanted to change the object passed in from main, you would need
to operate on *that* object... something like b.setValue(true), except
there's no such method! Boolean, like all of the primitive wrapper
objects, is immutable.
The behavior you're seeing on Linux is correct. I guess you might say
"pass by reference" is in the eye of the beholder. You're passing the
*value* of the local variable "bool", which is a *reference* to a
Boolean object, then expecting the value of the local variable to be
changed.
Nathan
Paul Grepps wrote:
>
> I expected the following to work since Objects are supposedly
> pass-by-reference. Am I doing something wrong here?
>
> I'm just trying to change a Boolean value inside of a method and
> return as pass-by-reference to the caller.
>
> My environment is:
> Blackdown JDK1.2 pre2, RedHat 6.1, glibc 2.1.2
>
> The output of the following program is:
> Boolean before: false
> changeBoolean(): false
> changeBoolean(): true
> Boolean after: false
>
> I expected the Boolean after: to be true.
>
> ----------------------------------------------------------------------
>
> public class BooleanTest2 {
> public void changeBoolean(Boolean b) {
>
> System.out.println("changeBoolean(): " + b.booleanValue());
> b = Boolean.TRUE;
> System.out.println("changeBoolean(): " + b.booleanValue());
> }
>
> public static void main(String args[])
> {
> BooleanTest2 bt = new BooleanTest2();
> Boolean bool = new Boolean(false);
>
> System.out.println("Boolean before: " + bool.booleanValue());
> bt.changeBoolean(bool);
> System.out.println("Boolean after: " + bool.booleanValue());
> }
> }
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]