aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can someone talk about the reference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can someone talk about the reference?" Watch "Can someone talk about the reference?" New topic
Author

Can someone talk about the reference?

Simon Sun
Greenhorn

Joined: Dec 07, 2000
Posts: 20
Hi pals,
I pop up the question about Madhu's code.
I borrow his code, hope he won't mind.
/*******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
My question is what the reference exactly is? As I understand it,
it refer the address of memory. some experts say that when we pass object in the arguments, we pass the copy of reference.
I am confused. the copy of reference is still pointing to the same memory location. so we change it, we change both.
So the both cases above should been changed.
Hope someone can throw me a light.
I really like this web site.
Thanks
Simon
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Simon:
a reference is just a pointer to the Object.
The Object lies somewhere in memory.
The closest analogy I can give is say Object is represented
like a City. A reference is the route you follow to get to
that City. Also, you should now realise that you can have
many routes to reach a City. Similarly, there can be many
references to an Object.
Having said that Object is a City and reference is a route, we see what is happening in the code abv:
Code 1:
btn route is created and it points to the
City GoodButton.
Then a call to the replacer() is made.
As you know, we will pass a copy of the
btn route to the function.
Now the function takes this replaceMe route
and points it to a new City Evil and returns.
However, please note that our original btn route and GoodButton City are not changed.
Code 2:
tf route is created and points to a City Yin.
Then we make call to the changer() function with a copy of the route calling the copied route as changeMe. Now since, route changeMe is a copy of route tf, it still points to the city Yin. Using this changeMe route, I reach the city Yin and change its name to Yang. So, now the Yin city name has changed to Yang. Then return.
After returning, we use the route tf to goto the city. And to our surprise find that the name of the city has changed from Yin to Yang, but still it is the same City.
All others, am I anywhere close.....
just imagining stuff.....pretty boring afternoon for me here, so trying to be creative.....
Regds.
- satya

[This message has been edited by Madhav Lakkapragada (edited January 10, 2001).]


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The campfire Story Pass by Value Please may help you understnad this


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone talk about the reference?
 
Similar Threads
Confused??? w/ Sample Code
what is the difference?????
Argument Passing question
Object refrences
argument passing