You have encountered the classic "call by reference / call by value" problem. Your code is written with the belief that a variable passed to a function will be altered by the function but in reality it is not. It is best illustrated by a simple code example:
Code:
public class Testing
{
public static void main (String[] args) {
int input = 23;
increment(input, 12);
System.out.println("Main: (1) input now: " + input);
// This time use the return value
input = increment(input, 12);
System.out.println("Main: (2) input now: " + input);
}
public static int increment(int input, int incrementValue) {
System.out.println("Input : " + input);
input += incrementValue;
System.out.println("Input now: " + input);
return input;
}
}
output:
Input : 23
Input now: 35
Main: (1) input now: 23
Input : 23
Input now: 35
Main: (2) input now: 35
Decent explanation here: http://www.yoda.arachsys.com/java/passing.html
Bookmarks