Hi Sarah,
This has been dicussed many times in this forum!
The answer is simply that
java passes values by reference which means that the reference variable itself can't be changed but the references objects' state can be changed.
The long answer:
1. Start with calling o.amethod
2. Set local variable i = 99
3. Create new ValHold: v (default: state of i = 10)
4. Change v state to i = 30
5. Call another method sending in v and local i
6. Inside another method get local v referencing the object pointed to by parameter v, and local i referencing the object pointed to by parameter i.
7. Change local i to 0
8. Change state of local v (pointing to same object as parameter v) to state of 20
9. Create new local ValHold variable: vh (default state of i = 10)
10. Assign local variable v to point to vh object
11. Print out local variable v state
10 (see 9&10) and print out local variable i
0 12. Return from another method
13. Print out variable v state
20 (see 8)
The main idea is that one objects can be referenced by two variables at the same time. Hence, if we change one (local v inside amethod) we also change the other (parameter v sent to amethod)!
Regards,
Manfred.