Scoping method arguments listing 26.11 & 26.12



I’m relatively new to java (and programming) and am trying to understand the scope of the items argument in the parseItems method.

parseItems(items, parser) is called inside fetchItems(), where ‘items’ is a local variable declared inside fetchItems(). Even though parseItems doesn’t return anything, it is able to modify the values stored in the local variable declared inside fetchItems() - this is something I’ve never seen before. Is this always the case in java?



Java is pass-by-value and objects are passed by reference, which in turn, are passed by value. Confusing enough? In this case, items is an object whose reference is passed into the called method by value. It can then be manipulated by calling its add method via its reference within the called method. After returning, it will have new items added to it. Have a look at some of the Java docs and tutorials online dealing with how Java passes arguments to methods.