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

Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
I got a mock question as following, the result should be 10, 0, 20. But I solve it with the result 10, 0, 30. the problem is why v.i become to 20 at last? How is the procedure make the v.i become into 20?
Thanks every one
Menghan
==========================================================
Given the following code what will be the output?
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

Ranch Hand
Posts: 36
• Number of slices to send:
Optional 'thank-you' note:
Consider:
public void another(ValHold v, int i){
// ! Copy of the passed value i is changed to 0. Method amethod()'s i still the same.
i=0;
// ! Object variable i refered to by both another()'s v and amethod()'s v is changed.
// ! Remember a reference to the object v got passed into another().
v.i = 20;
// ! Now create a new object vh
ValHold vh = new ValHold();
// ! Point another()'s v to the new object. But amethod's v still points to its the original object.
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
When another() returns to amethod(), amethod's v still points to the original object whose i value was changed to 20 in another(), to thats what prints.

Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
Dear Daryl,
============================================================
ValHold vh = new ValHold();
// ! Point another()'s v to the new object. But amethod's v still points to its the original object.
v = vh;
============================================================
I agree with you but
I'm still a little confused about one thing, the statement
set v to point to vh, the newly created instance; are you saying
that v as a reference is passed-by-value therefore whatever you
do inside the method another() will not affect the original v in
chengx

daryl olson
Ranch Hand
Posts: 36
• Number of slices to send:
Optional 'thank-you' note:
The call in amethod():
another(v,i);
passes a copy of the reference to the object (not the object) and a copy of the int value of i (which I believe you know).
In another(), a copy of this refernce now points at the original object.
amethod's v -----> |object of type ValHold|
another's v ----------^
so that the line in another:
v.i = 20;
changes the value i in the original object. Then:
ValHold vh = new ValHold();
v = vh;
creates a new object of type ValHold and sets the copied reference to point to it. The variable v in amethod() still points to the original object. So now we have:
amethod's v -----> |object of type ValHold|
another's vh ----> |another object of type ValHold|
another's v -----------^
Does this make sense?

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?