☰ Contents How Java Runs

Objects, Methods, and Memory

Student: I think I still don’t get what’s going on with stack frames and method calls. What is the reference in the top of the stack frame for?
Prof: Let’s build up an example with two objects whose methods we call.
Student: Hmm… so there’s like… three frames? One for each time discountInPlace is called?
Prof: Makes sense. So how are they different? The method doesn’t take any arguments, so what makes the frames different from one another?
Student: Oh! So to tell apart the first two calls, it has to know which object is used for each, menu or pricyMenu.
Prof: Exactly.
Student: That’s cool. So the first call uses menu, the second uses pricyMenu. Oh and the values in the menu one get updated twice. Why is that?
Prof: That corresponds to the two calls to discountInPlace on menu. The first time through, it updates all the values once. The second time is still working with the same array, so it updates it again.
Student: I see!

Practice

This record has a builder and an updater too. Step through the trace to check your answers.

How many times does the value at index 0 of morningScores change while this program runs?

How many times does the value at index 0 of eveningScores change while this program runs?

How many arrays does this program create in total?