wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object refrences Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object refrences" Watch "Object refrences" New topic

Object refrences

Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
This example is from page 20,Roberts and Heller.
Will somebody please explain me the concept of object and object refrence.

Button btn;
btn = new Button("Pink");
public void replacer(Button replaceMe)
replaceMe = new Button("Blue");

Button btn;
btn = new Button("Pink");
public void changer(Button replaceMe)
Sunil K Bansal
Ranch Hand

Joined: Jan 04, 2001
Posts: 62
coz a reference variable at time can point to one object only
in the first case replaceme variable now refers to new Button(blue) object but the original button(pink) object still exists
and is refered by the btn reference variable
so the btn is pointing to button(pink) only
in second case change me is pointing to the object button(pink)
and it changes a property(label) of the same object
so the property of the original object is changed
which is refered by the btn
so we see the changes in the same object

Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Vineet,
I don't have the book however, from the example it looks as if they are trying to illustrate how Java passes variables to methods as copies ie it does not pass the original object.
In the first example,
<code>Button btn;</code>
creates a Object reference variable named btn.
<code>btn = new Button("Pink");</code>
creates a new Button object with the label "Pink" and stores the memory address for the new object in the reference variable btn
calls the method replacer passing it a copy of the reference variable btn.
In the replacer() method
<code>replaceMe = new Button("Blue");</code>
creates a new Button object with the label "Blue" and stores the memory address of the new object in the reference variable replaceMe
Program control then returns to the original method which prints out the button label. What gets printed? "Pink" ... Why? ... because the original value of the btn reference was NOT changed in the replacer() method ... a copy of the orignal reference was changed.
In the second example, within the changer() method
the changeMe variable is also a copy of the original reference ... it points to the original Button object which had the label "Pink". When setLabel("Blue") is called, it affects the Button object being pointed to. So when the method completes and program control returns to the calling method which prints out the btn label what gets printed? "Blue".
In the first example, the original object is not affected because only a copy of the reference was changed; in the second example, the object itself was affected because the copy of the reference still pointed to the original object.
There is an article Understanding that parameters are passed by value and not by reference that may make this point clear.
Hope that helps.
PS ... I'm going to move this to the Programmer Certification discussion as it's not really a mock exam error.

[This message has been edited by Jane Griscti (edited January 06, 2001).]

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
I agree. Here's the link: http://aspose.com/file-tools
subject: Object refrences
It's not a secret anymore!