• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

passing reference in methods

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is from Marcus Green's exam 2.
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.println(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.println(v.i+ " "+i);
}//End of another
}
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
The answer is 4 .
I understand part of it.The another() will print v.i =10 & i = 0.
But I don't get how come v.i becomes 20.
Please explain me the concept in simple words.
Thank a lot.
Mita
 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the sticking point here is that methods are passed copies of references, not the original references themselves. So in the method another, the ValHold v is a copy of the original reference. So long as you don't change what the ValHold v is a reference to in another(), it will change the original object. the line
v.i = 20;
is still a reference to the original object. But after the line
v = vh;
v no longer refers to the original reference. The copy has changed what it points to but this does nothing to change the original reference or object.
OK. Now, the reason this prints twenty is because the final value printed is in the method amethod() not another(). If you go back to amethod(), the v there still refers to the original object. The assignments in the method another() have no impact on this.
Does this help? Feel free to ask a follow-up.
Eric B.
 
mita
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Eric,
I have another doubt.Tell me if we don't put v.i= 20 inside the another method, then would the answer be 10, 0,30 not 10, 0, 20.
I have seen in some places the values get changed and in some places they don't.
If the copies get passed, then when(what condition ), the reference value gets changed.
I know the primitive value doesn't get changed at all.But what is the catch with reference variables.
Is there a difference when the method is called by a reference and when the method is called by itself (like another()).
Please explain.
Thanks, I appreciate it.
mita
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty sure if you omit v.i = 20 in another() ,the last value printed would be 30.
Reason is that when you type v.i = 20 , the v is the copy of reference of the v reference in amethod().
The bottom line is that although it's a copy of the reference, v in another() still refers to the original object in amethod(). when v = vh is set, this is not true anymore b/c now v refers to object pointed by vh. however, since v is only copy of the reference, it doesnt affect original object in amethod().
confusing right? =)
please anyone feel free to explain differently
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
There is a concept of passby value and passby reference. When
you pass a primitive type it is passed by value. What i mean
by this is a copy of the original parameter is passed to the
function. Now lets supposed
main()
{
int a = 7;
somefunction(a);
}
somefunction(int b)
{
print(b);
}
In this case a copy of the value of a which happens
to be 7 is passed to the function somefunction.
ie there are two different symbols a and b which
have the same value 7.
But when u pass an object it is pass by reference.
By this i mean that the location (address) where the
object resides is being passed to the function. So once
i get the address in the function i can look into that object and
modify it contents.
main()
{
Object obj;
somefunction(obj);
}
somefunction(Object localobj)
{
int newValue = 10;
localobj.val = newValue;
}
Thats what u are doing with your
v.i=20. So effectively you are changing the contents
of the object. Since obj and localobj are looking
at the same address.
But then when you assign the localobj with a new
obj then localobj will now look(point) to a different
location(address). (Please remember the original object is still
in place). You are just making your localobj to point to
some other object ie location/address.
ValHold v = new ValHold();
And now whatever you do with this local object is local to you function.
Infact it will be garbage collected once you return from you function
since it is no longer in the scope.
Hence before the assignment
ValHold v = new ValHold;
there is only one object under consideration which is being modified and after
the assignment there are two completely independent object and you
are looking at the newly created one.
I hope you understand this. Iam sorry about the syntax since iam not quite
particular about it and also iam still not comfortable with Java (Iam
from a C/C++ background)
RK
 
Eric Barnhill
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mita,
your doubt is right. if you eliminated that line the answer would be 30 and not 20. There were some helpful posts that followed mine. I hope it's clearer now.
Eric
 
mita
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much Eric, RK and Agi.It is definitely more clear but I need to practice.
Thanks
mita
 
reply
    Bookmark Topic Watch Topic
  • New Topic