[Re-writing my question, due to ambiguity.]
Why is it that I can instantiate a HashMap and a Boolean (or even an Integer), pass them to a recursive helper function, peek at my various stack frames and "see" that operations on the HashMap in one frame are detectable in different frames, but I can't also see changes to the Boolean in different frames?
My goal is to be able have a "fast runner" make it to the end of a tree path, while a slower one awaits a "signal" that the "end was reached" and to return whatever node it's on. I could obviously use the hashmap, but why can't I simply pass an Integer (not int), a Boolean, etc.
In my code here, I simulate using a simple counter to recurse 5 times, deposit "3" into a hashmap, and when stack frame 3 "sees" the entry in the hashmap, it knows to return -1, not 0. THIS WORKS FINE. I'm confused why the Boolean baseCaseHit stays FALSE in all but the 5th frame, where it's flipped to TRUE.
static int recursionTest(Integer counter) { // assume 1 is passed in
Boolean ok = false;
return recursionTestHelper(counter, ok, new HashMap<Integer, Integer>());
}
// Goal is to recurse 5 times, growing counter, and after two stack frame pops, return -1, else 0
static int recursionTestHelper(int counter, Boolean baseCaseHit,
HashMap<Integer, Integer> memo) {
if (counter == 5) { // Let this recurse 5 time
**baseCaseHit** = true; // This Boolean only changes in frame #5
memo.put(0, 3); // Store 3 at key 0, for stack frame 3 to catch later.
}
else {
int result = recursionTestHelper(counter + 1, baseCaseHit, memo);
}
int stackFrameToImpact = memo.get(0);
if (counter == stackFrameToImpact) // *This works fine.*
return -1; // This signals that all worked as hoped.
return 0; // This signals that we're still just popping off stack frames.
}
public static void main(String[] args) {
System.out.println( recursionTest(1) );
}