Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

question from marcus green  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 correct answer here is 10,0,20
shudnt it be 10,0,10?
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The answer is 10,0,10 which is the right answer. Don't get confused by the method calls. The guide to the answer is this:
In another(), i is local to this method. So it prints 0.
v.i = 20;// set v.i =20
ValHold vh = new ValHold(); // create new ValHold object with vh.i =10
v = vh; // set whatever reference v to vh -> now vh can also be reffered as v
System.out.println(v.i+ " "+i); // v.i is 10 from last step.
Hope this helps

Cheers
Kapil
 
kapil apshankar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops!
I just messed up the last answer!
The right answer is 10,0,20.
The explanation for 10,0 is still correct. Don't forget that i is a primitive data type. So it is getting passed as a copy of the original value.
So once you change v.i to 20, it will remain 20 whereever you access it.
Please correct me if I am wrong.
Cheers
Kapil
 
Arun Misra
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the 10, 0 part
but once u made
v = vh;
then V is poinint to same addr as VH
and then we printed v.i = 10 and i = 0
once we go back to caller V shud still point to same addr?
so shudnt it be v.i = 10 for the second time?
which means answer shud be 10,0,10
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I ran and compiled this program so I am certain the answer is:
10 0
20
The confusion lies in the statement:
ValHold vh = new ValHold();
v = vh;
Two ValHold objects have been created( hence the v.i = 10 part ), but I don't have a definate answer as to why the v.i in amethod() has the value of 20 when another() completes.
My guess is that the first object's v.i was changed to 20 in another, but the second object's v.i was changed after creation leaving the first object's v.i unchanged. However, the cariable v seems to reference 2 obects( I think ) and I don't even know if this is possible. Somewhere the 2 objects have different values, but the same reference. I really want to know the answer to this, so please, please anyone correct me / us.
Thank you for listening to my confusion.
 
Slightly Uber
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops, sorry:
My guess is that the first object's v.i was changed to 20 in another, but the second object's v.i was changed after creation leaving the first object's v.i unchanged
is completely wrong:
the second object's v.i is not changed at all. so the first object's v.i is changed to 20, and the second object's v.i is 10.
Boy, I am really confused =)
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there
woudn't u think the problem is that vh goes out of scope after another();?
public void another(ValHold v, int i){
i=0;
v.i = 20; <== refers to orignal object created in amethod()
ValHold vh = new ValHold(); <==new object
v = vh; <== refers to new object
System.out.println(v.i+ " "+i); <== so prints 10,0
}//End of another and vh goes out of scope
this is what i think!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Can I try ? v.i was set to 30 and then v was passed to method another. This method changed the value of v.i to 20.Is this correct ?
eric

[This message has been edited by Eric Rowan (edited March 21, 2001).]
 
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if this will help or confuse this issue but. . .



[This message has been edited by Cindy Glass (edited March 21, 2001).]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The last statement is still not clear. If the contents of v were modified in the method to 10, then the last number printed should be 10. Can someone explain this in terms of how the values are passed ?
Thanks!
 
sajida
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I found the answer to this question which was posted in the same forum on March 15th- the subject is 'real confusing stuff'.
The answer is explained in terms of how the arguments to a method are passed by value, so once the method has finished execution, the changed value is not in scope. You can check
out the answer posted to the above question.
cheers
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just to clarify for myself and you (hope I am right)
sajidas mentioned other thread points to: parameter Passing
Its really good stuff. If you pass a object to a method you pass a COPY OF THE REFERENCE of the object is passed.
In another(ValHold v, int i) v points to the same object as v in amethod.
If you change the instance member variable v.i to 20. O.k. the value is changed. If you assign v to a new object in another(valHold v, int i) the reference v holds is changed in another BUT NOT IN amethod. The new object is constructed and v.i is 10.
And if you return to amethod v.i is 20, because the instance member variable was changed in amethod.
Seems clear. Am I right???
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
Here is the right answer....
10 0
20
public void amethod(){
int i = 99;
ValHold v = new ValHold();
// assume that the refernce v, hold the address 1000 of the obj.
v.i=30;
// i = 30
another(v,i); ----------------
System.out.println(v.i); |
}//End of amethod |
|
public void another(ValHold v, int i){
// a new refernce is created called v which also has the address 1000, thus we have two references to the same object
i=0;
v.i = 20;
// i = 20
ValHold vh = new ValHold();
// a new object is created with a refernce called vh
// assume that vh carry the address 2000
v = vh;
// v now carry the address 2000, the trick is here !!
// the address of local v is changed but that of function amethod is still pointing to the address 1000 where i=20
System.out.println(v.i+ " "+i);
}//End of another
Contact me for any further information.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sajida,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for you cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Axell,
The stuff you mentioned on parameters passing was really good.
Thanks.
Cindy,
as you can see, I have reregistered. Thanks!
 
Arun Misra
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all of u for expalining this!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!