Hi All
I would like to get help on the following
Question 51)
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
Answer Objective 5.4)
4) 10,0,20
In the call
another(v,i);
A reference to v is passed and thus any changes will be intact after this call.
1. In method another v is assigned by vh a new instance of ValHold. So v.i will be 10.
2. i is a local variabl in method another. So its value will be 0 ( as initialized in method another ).
3. Here is my doubt. Why does the System.out.println(v.i); in amethod print 20 ? If the reference has been assigned by vh in method another here also this should be 10 ??? Really confused. Help please !!! Thanks
ARS Kumar