wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes argument passing 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 "argument passing" Watch "argument passing" New topic
Author

argument passing

luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Hello, all,
It is very difficult for me to understand the following examples of object arguments passing in Simon's book:
Example1:
Button btn;
btn = new Button("Pink");
replacer(btn);
System.out.println(btn.getLabel());
public void replacer(Button replaceMe) {
replaceMe = new button("Blue");
}
The author said in this example, the value printed out is still Pink, not Blue.
Example 2:
Button btn;
btn = new Button("Pink");
changer(btn);
System.out.println(btn.getLabel());
pbulic void changer(Button replaceMe){
changeMe.setLable("Blue"));
}
The author said that the printed value for this code is Blue.
My understanding is that when an argument is a primitive data type and passed into a method, changes to the argument value by the method do not affect the original data; while if it is object reference, the original value will be affected when passing value by method call.
I am not sure whether my understanding is correct.
Thanks! luk
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
Hi Luk,
I'll try to exmplain. Let's look at first one:
Button btn;
btn = new Button("Pink");
replacer(btn);
System.out.println(btn.getLabel());
public void replacer(Button replaceMe) {
replaceMe = new button("Blue");
}
See what's happening is this. btn is a reference to a Button with label pink. WHen method replaces is called it passes it a reference btn. But look what's happening there. btn is assigned to a replaceMe now it points to that same Button object right but look there it's been assigned a new instance of another button so it has not modified the object that btn is still point too. If it would have called method setLabel replaceMe.setLabel("wahtever") now the label that btn object points to would have been modified. But it's not it just get's assgined to point to a new created object.

Example 2:
Button btn;
btn = new Button("Pink");
changer(btn);
System.out.println(btn.getLabel());
pbulic void changer(Button replaceMe){
changeMe.setLable("Blue")); // shouldn't changeMe be replaceMe instead ?
}
See that's what i talked about in earlier comment . This is the exact behaviour that's happening here.

Val SCJP <BR>going for SCJD
luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Hi, val,
Thanks so much for your quick reply and answer.
I thought I was still a little bit "clear as mud". Could you illustrate the following from another angle?
(btn is assigned to a replaceMe now it points to that same Button object right but look there it's been assigned a new instance of another button so it has not modified the object that btn is still point too).
Can I say that the object Button has two references: btn and replaceMe with two values: "pink" and "blue". I think something did affect the result here are getLable and setLable method. Using only new constructor could not pass a value to btn, while using setLable method could. Is that right?
In Simon's explanation, he also mentioned "caller" and "callee". Which is caller, which is callee in this example?
Finally, what happened with array?
Sorry to answer so many questions and thanks so much in advance!
Luk

Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
I hope this diagram helps out.
variable replaceme variable Btn
| |
____________________________
| physical object Button |
| |
| has a label property |
|___________________________|
Notice that replaceme and Btn are only variables that point to
the physical object and the physical object has a property say
a label property.
In passing parameters for objects, only a copy of the address
is sent. So replaceme is just a pointer to the physical
object.
When modifying passing object parameters in this case
replaceme, the address of replaceme cannot be changed. That's
the catch the physical object that replaceme points to
cannot change. But the properties of the object pointed to
can change. That is the reason why the label can be changed,
and a new button cannot be created.
When a new button is created, and replaceme points to it, it
changes the original address of the object.

Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55
Thanks for this topic. I to had problem with the exact same question.

------------------
Preparnig for the Java 2 Certification exam


Preparing for the Java 2 Certification exam
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Charlie, I agree with everything you said except maybe your final sentence:

When a new button is created, and replaceme points to it, it
changes the original address of the object.

The original address of the object (the button) is not being changed; rather, the address which the reference variable replaceMe is pointing to is changed to point to a new button (which has no effect on the original button object). And since you've now lost your only pointer to the original button within the replacer() method, nothing that follows in that method can possibly affect the original button.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: argument passing