|
 |
|
 |
05-05-2004, 07:57 AM
|
#1 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
passing by reference
ok, i'm about to go read up on this, but i thought i'd post this question so when i find the resolution it will help someone in the future.
in my class i have this method:
Code:
public boolean setCustomerByID(customer cust,String id)
{
// if found set customer object
return true;
// if customer not found
return false;
}
i need this method to return a boolean value, and this is how i want to call it:
Code:
customer c = new customer();
if( myclas.setCustomerByID(c,"1234") ){
out.println(c.firstName + " " c.lastName);
}else{
out.println("customer not found");
}
in that example, i would like the setByCustomerID method to return true/false, but i alway want my new customer object 'c' to be set in the process.
at this point, it doesn't seem like customer is passing by reference.
i'm going to go google it now but any insight would be appreciated. i'll post my findings later.
__________________
Mike
|
|
|
05-05-2004, 08:15 AM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
ok, am i correct in assuming the reference will hold until i assign the object to a new object inside the method?
here's the best example i found:
Code:
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.print( v.i );
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.print(v.i);
System.out.print(i);
}//End of another
}
__________________
Mike
|
|
|
05-05-2004, 08:17 AM
|
#3 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
I dont know Java but I found this interesting:
http://www.yoda.arachsys.com/java/passing.html
From there you can build further I think. I'm cetain it will give you an edge.
__________________
|
|
|
05-05-2004, 08:35 AM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
So in your code, "c" is nothng but an "object reference". So when you pass it to your ID function, you will pass a COPY OF THE REFERENCE.
Aha, now I see what Java is about  .
So you could "alter" the value(s) in "c" but you cannot interchange it with other objects if passed to the same function (like a "b" for example).
You need to use the original references for that.
That's what I understood from that text.
So try your experiment like this:
- Make a swap function.
- Give objects b and c a value in main. (your current customer objects will do just fine).
- pass them to your swap function.
- swap them.
- test in main: fails.
- in swap function: assign new value (name and id) to "c".
- test in main: success, value of c has changed.
Watch it, strings are passed by VALUE. No copies of references are made.
And it is certainly not a reference like in C++.
That's what I understood from that story. But you gotta test it in the swap function I proposed. I don't have Java.
__________________
|
|
|
05-05-2004, 08:38 AM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
So
should fail if you test it in main().
That's what I understood. You just copied a copy of a reference. Not the object reference itsefl.
I wonder if I was even close to the facts.
If I am, give me a candy  .
__________________
|
|
|
05-05-2004, 08:43 AM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Quote:
|
...will hold until i assign the object to a new object inside the method?
|
That's right.
We're in sync. If you're wrong (the java experts will come soon hopefully), then I hope you didn't catch some of my stupidity  .
__________________
|
|
|
05-05-2004, 09:44 AM
|
#7 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
I didn't quite follow what you were trying to do, but in Java, Val is correct in that as far as Objects, a copy of the reference was passed. That means you're still working on the same object that was passed in. Primitives are passed by value, so changing an int inside a method won't change the value of that int at wherever it is outside of that method.
|
|
|
05-05-2004, 10:24 AM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
well my method searches a database and creates a new customer object.
when i assign the new customer object to the object i passed in the method, .. the new value is not accessable from outside the method.
for now i just work-around by returning the customer, .. but i'd rather make the method boolean and assign a new value to the object inside the method.
__________________
Mike
|
|
|
05-05-2004, 10:26 AM
|
#9 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
That's what I understood too. But I have a question as well now, to test my theory (I don't have java remember?):
If a primitive (like an int) defines the state of an object then code like
Code:
a.myIntegerValue = 5;
inside a function would change the the state of an object and therefore
Code:
out.print(a.myIntegerValue);
will yield "5" outside the function?
__________________
|
|
|
05-05-2004, 12:06 PM
|
#10 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
i think i have this down now .. are my comments correct here?
Code:
private void doSomething(MyObject myObj){
// this is the same object that got passed in
myObj.param = 1;
MyObject newObj = new MyObject();
newObj.param = 2;
myObj = newObj;
// at this point, the object passed in
// from outside 'param' = 1 and i can
// no longer maniuplate the object passed in
}
sorry to beat a dead horse, but is that right?
__________________
Mike
|
|
|
05-05-2004, 12:16 PM
|
#11 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Once you exit "doSomething", the result of this:
will be discarded.
__________________
|
|
|
05-05-2004, 12:31 PM
|
#12 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
because nothing will be using myObj anymore as we knew it inside doSomething ..  ok, it makes much more sense now.
__________________
Mike
|
|
|
05-05-2004, 01:20 PM
|
#13 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
Val, you're correct both times. Because the method passes a copy to the reference, as opposed to the reference itself, you cannot reassign the reference. Hence, setting the input to a new object won't affect the object outside the method.
sde, why not just return null instead of false, and check to see if the object returned is null?
|
|
|
05-06-2004, 06:22 AM
|
#14 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
|
Quote:
|
because nothing will be using myObj anymore as we knew it inside doSomething...
|
Inaccurate sde, sorry.
You never used myObj to start with...
You used a copy of myObj's reference. Not myObj.
There were two myObj-references in your program. Your function used one of them: the body-double in the world of memories...
Hey! I gotta remember this one...
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 01:08 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|