Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

what is the difference?????

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
[
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic