Hi,
Lets see the program code line by line.
1. class ValHold {
2. public int i = 10;
3. }
4.
5. public class Obparam {
6. public static void main (
String args[]) {
7. Obparam o = new Obparam();
8. o.amethod();
9. }
10.
11. public void amethod ( ){
12. int i=0;
13. ValHold v = new ValHold();
14. v.i=345;
15. another(v,i);
16. System.out.println(v.i);
17. }
18.
19. public void another(ValHold v, int i){
20. i=0;
21. v.i=20;
22. System.out.println(v.i);
23. ValHold vh = new ValHold();
24. v = vh;
25. System.out.println(v.i);
26. System.out.println(i);
27. }//End of another
28. }
At line number 15 the ValHold object V is passed inside another() method. As obvious it will pass the referrence of the V object (as V is not a primitive data type). So if we change the state of the V (value of the instance variables of V object) inside another() method, it will reflect the V object. Because inside another() method it is using the same refference of V.
In line 21 we are changing the value of v.i (instance variable of V) to 20. So in line 22 it will print 20.
In line 23 we are creating another new ValHold object vh. By default the instance vairiable i of vh object will be initialized with 10 (ref. line no 2). In line number 24 we are copping the referrence of vh object into the object refference variable v. So after line number 24 v and vh both reference variables are refferencing to the same Object (object created at line number 23).
So in line 25 it will print 10 (The value of i of vh object)
At line 26 it prints the value of i, which is the parameter of the another()method. The value of i is passed from amethod() function (line number 15). The actual value of i in amethod() is 0 (line number 12). So inside another() method at line 26 it will print 0.
After completing another() method, the control will return back to amethod(). And it will print the v.i at line 16. At line 16 it will print 20, the value of v.i, which has been changed inside another() method (Line 21).