Paul Grepps wrote:
> 
> I expected the following to work since Objects are supposedly
> pass-by-reference.  Am I doing something wrong here?

Yes :-)
 
> I'm just trying to change a Boolean value inside of a method and
> return as pass-by-reference to the caller.

in changeBoolean() you are not changing the state of the passed Boolean
object. Instead you're assigning something to the reference of the
object, which itself is a copy of the original reference.

Something like this would work:

  public void changeBoolean(Boolean b) {
    b.setValue(true);
  }

but unfortunately objects of type Boolean are not mutable (you cannot
change the value of the wrapped boolean value).

I would suggest to write your own class (e.g. MutableBoolean or
BooleanHolder) which allows for changing of the wrapped type.

Regards, Oliver

> 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]

Reply via email to