Hi Pallavi,
I think you have an extra closing bracket up top, which puts your methods outside the class...I think thats a typo. I also think the your out put would be 3010020.
Ok, here are the steps:
1. Obparm o = new Obparm(); creates and instance of Obparm.
2.o.amethod(); Lets get into the amethod()
amethod() 3. int i = 99;
4. ValHold v = new ValHold();makes a new instance of v. So now v.i is back to 10.
5. v.i=30; Ok, are now over writing v.i with 30.
6. System.out.print(v.i); - This should print the first number
30 7. another(v,i); Call another() method with v which refers to the 30 guy and i which is still 99.
another() 8. i=0; Local copy of i is not 0.
9. v.i = 20; Here we are seeing the actual data in v to 20. Even though v is only a copy of the reference, it still points to the same object. So it sets the i to 20 in the original instance.
10. ValHold vh = new ValHold(); What we did here is made a new instance of ValHold. So vh.i = 10.
11. v = vh; Ok here we are changing the copy of the referance we got, to point to the new instance we created. Only the copy is being changed. The original referance is still there.
So now v.i = vh.i = 10
12. System.out.print(v.i); Here you get the second number
10 13. System.out.print(i); Here you get the third number
0 coz the local copy of is 0.
14. Now back to
amethod() 15. System.out.print("the last one" + v.i ); Here you get your last number
20. This is because you original copy of v in another method still refers to the original ValHold instance. But remember we changed the data in this instance to 20. See step 9.
Hope this helps.
Monisha