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
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 <code>replacer(btn);</code> 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 <code>changeMe.setLabel("Blue")</code> 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. Jane 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).]