Sorry John - its quite a long winded response. The key thing to remember is that java passes everything by value and nothing by reference.

In this diagram (which I hope works on posting)each horizontal section represents a new set of values on the stack. Objects are never passed to methods, only references to objects are passed to methods. This is demonstrated in line 24 where the value held in "val", a reference to a new instance of Value() is assigned to the reference variable "v". This results in "v" holding a reference to the new instance of Value(). It does not have any impact on the instance of Value() previously referenced by this local variable. so : - first() has an int called i with a value of 5 and an instance of Value() called v. - second() has another int also called i which is a parameter variable and so is assigned the value passed on the call. This int's value is subsequently changed to be 0 on line 21. There is also a parameter variable which is a reference to a Value() instance. This variable holds a reference to the instance of Value() created on line 13. On line 22 we use this reference variable to change the value of i held in the instance of Value() to be 20. On line 24 we then change the value of the reference variable to refer to a new instance of Value().

It is important to distinguish between references to objects and the objects themselves. Something that is well illustraded by your code example. I hope this helps, Malcolm

Wow, that's a neat way to illustrate it. I try to remember that Java always makes a COPY of the variable being passed. If you call a method with a primitive argument, say an int variable, Java makes a copy of the variable. If the method changes the int parameter (flagged as an error by some) it changes its own copy, so the original is not changed. If you call a method with an object argument, say HashSet, Java makes a copy of the variable again. But this time the variable is a pointer to an object. If the method changes the pointer to a different Set, it only changes its own copy of the pointer and the original pointer is not changed. Exactly like the int. What's different is that the copy of the pointer points to the same HashSet as the original pointer. If the method adds or removes elements in the Set, the original Set is modified. That's nothing like the int example, because the int was not a pointer. At the beginning I said Java always makes a copy, which is what makes it "by value". Copy sounds expensive, but recall it only copies primitives like int, and pointers which are also just a few bytes. Not a big deal.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

I think you're confusing yourself quite a bit by using the same variable name too much. If you would change the second() method to: public void second(Value x, int i) { i = 0; x.i =20; Value val=new Value(); x= val; System.out.println(x.i +" " + i); } This preforms exactly the same operation, but I think the procedure would be a lot clearer to you.