Originally posted by jaya S K:
Please take a look at this program.
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.print(v.i);
}
public void another(ValHold v, int i)
{ i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.print(v.i);
System.out.print(i);
}}
The output is 10020
Can anyone explain this
Output is 10020 which is right. This is how it works
10 ==>
ValHold vh = new ValHold(); // Initialise i = 10
v = vh;
System.out.print(v.i);
0 ==>
i = 0;// parameter passed to the function "another" which is set to 0
....
System.out.print(i); // changed parameter value printed
20 ==>
System.out.print(v.i); // "another" method changes its value "v.i = 20; "
Hope this helps
V