• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q from Marcus ex-2?

 
sri devij
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
The ans is given as 4.How is it possible and why?Can anybody there clear my doubt?

Thanx in advance
 
Edy, Yu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your class ValHold?
 
Mary Anitha
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you pass an object reference as a parameter
the changes made to the "object" will affect the object (v.i)
but
the changes to "object reference" will not be affect it.(v will not change)
object is passed by reference
object reference is passed by value

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);

/* here after coming back from the method another v=vh does not hold good. v still points to the same old object.
But the object's i value was changed in "another" method so v.i=20 */
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20; //this changes the value of i of the object that is passed
ValHold vh = new ValHold();
v = vh;// this does not change the value of v when the execution returns to amethod
System.out.println(v.i+ " "+i);
// v.i=10 as vh.i=10 ; this is true only in this method ;i=0 local variable
}//End of another
}
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
So the answer is 4 .
[This message has been edited by Mary Anitha (edited October 24, 2000).]
[This message has been edited by Mary Anitha (edited October 24, 2000).]
 
Amit Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I studied the code...and as per my reading the answer should be 2>20,0,30....
please correct,if I am wrong...
Thanks
Amit
 
Mary Anitha
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit
I think u missed v=vh in the following method
when vh was created vh.i=10
when v is made equal to vh
v.i will have 10 but in this method only, not after returning from this method
In line 3 the value of i was changed which is reflected in the calling object.
1) public void another(ValHold v, int i){
2) i=0;
3) v.i = 20;
4) ValHold vh = new ValHold();
5) v = vh;
6) System.out.println(v.i+ " "+i);
7) }//End of another
 
Amit Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anitha,
Thanks... I really missed that important point....

Bye
Amit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic