public class gas_station { int start = 0; public int commonMethod (int[] gas, int[] cost) { int len = gas.length; while (this.start < len) { if (canComplete(this.start, gas, cost)) { return this.start; } } return -1; } public boolean canComplete (int index, int[] gas, int[] cost) { int len = gas.length, sum = 0; for (int i = index; i < len + index; i++) { sum += gas[i % len] - cost[i % len]; if (sum < 0) { this.start = i + 1; return false; } } return true; } } As you can see, in the canComplete function, I make changes to class member start, and in the commonMethod function, I use it as the loop variable, my question is, is there a way I can make change in the canComplete function and influence the variable in the caller function (here, commonMethod function) instead of making a variable (start) a class member, I know in C++, I can pass a pointer to a function, but what should I do in Java? Thanks!
Integeris just a wrapper, and is immutable, but you could pass an array or anAtomicInteger