Hi Paul,

"objects are passed by reference" is only half of the truth. In fact,
this is implemented by passing the reference to the object (in C you
would call it a pointer) by value. Your parameter Boolean b in
changeBoolean() is this reference. By assigning to the reference, this
reference will furthermore reference (point to in C-speak) another
object. The object itself, of course, is not changed. To change the
object, it has to have a method to influence its internal state. In case
of a Boolean object, a meaningful method would be setValue(boolean). For
security reasons and to make Boolean objects as similar as possible to
the native type boolean, such a method does not exist.

Matthias


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]

Reply via email to