wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the difference????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the difference?????" Watch "what is the difference?????" New topic
Author

what is the difference?????

madhu kumar
Greenhorn

Joined: Jan 05, 2001
Posts: 20
Hi,
this is a query for passing of object references as argument and passing a variable.
look at the following code[quoted from rhe]
/*******code1******/
Button btn;
btn=new Button("Good");
replacer(btn);
System.out.println("btn.getLabel());
public void replacer(Button replaceMe){
replaceMe=new Button("Evil");
}
/******output is Good
/*******code2******/
TextField tf;
tf=new TextField("Yin");
changer(tf);
System.out.println(tf.getLabel);
public void changer(TextField changeMe){
changeMe.setText("Yang");
}
/*****output is Yang
I understand that methods cannot change the original values of the arguments since they are only their references but how come
the textfield in code2 is changed!!
Iam confused!!!
Pratap Reddy
Ranch Hand

Joined: Jan 05, 2001
Posts: 36
In your first example you trying to creating a new object called replaceMe. But in your second example some thing different changing the as follows,
TextField tf
tf=new TextField("Yin");
changer(tf);
System.out.println(tf.getLabel);
public void changer(TextField changeMe)
{
changeMe = new TextField("Evil");
}
Then you will get "Yin" as output.
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
I'll try to answer this:

when you pass a reference of an object to a method call, (in the first case:btn ), you actually pass a copy of the reference, the new reference is "replaceMe", on this point we have two reference to the same object (created with new Button("Good"); ), but when you assign a new object to the second reference, the first remains unchanged

On the other code -code 2-, both references have the same object reference ( created with new TextField("Yin") , and thus, both can modify its attributes and access its methods, in this case second referece "changeMe" performed the setText(String) method, and the first reference reflects the canges too, because there is only one object and two references to the same object.


Consider this code:

<pre>
1. public class ReferenceObject {
2. int attribute= 0;
3. void aMethod(){
4. attribute = attribute + 1 ;
5. }
6. public static void main(String args[]){
7. ReferenceObject one = new ReferenceObject();
8. ReferenceObject two = one;
9. System.out.println(one.attribute);
10. two.aMethod();
11. System.out.println(one.attribute);
}
}
</pre>
output:
0
1

The reference "two", change the "attribute" of the object created on line no. 7 "new ReferenceObject();"
and the reference one , (reference to the same object), can show us in lines 9 and 11.

If in the other hand you create two objects well...

see next code
<pre>
1. public class ReferenceObject {
2. int attribute= 0;
3. void aMethod(){
4. attribute = attribute + 1 ;
5. }
6. public static void main(String args[]){
7. ReferenceObject one = new ReferenceObject();
8. ReferenceObject two = new ReferenceObject();
9. System.out.println(one.attribute);
10. two.aMethod();
11. System.out.println(one.attribute);
}
}
</pre>
output:
0
0

In this case reference two change the attribute of a second object, and thus object of reference "one", remains unchanged

Ufff!!!, I hope this modest explanation make thins clear,

and not confuse you more.
)


If anybody can further my explanation and/or correct me, you're welcome.
[
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: what is the difference?????
 
Similar Threads
Argument Passing question
Object refrences
argument passing
Can someone talk about the reference?
Confused??? w/ Sample Code