Have students act out diagrams they create of the stack and heap to extend this common drawing exercise and help them strengthen their mental models.

  • Draw a picture of the stack and the heap in order to help students understand:
    • what really happens when they write their code.
    • concepts like "mutable" and "deep vs. shallow copy."
  • Sometimes students will say they understand the stack and heap diagrams, but they have an ah-ha moment after acting out how variables are passed between methods.
  • Activity Prep:
    • Create a paper drawing or cutout of a cat.
    • You could use something else to represent the variable, the using a cat to represent a cat will help students make the connection.
    • Create a stack of papers that says "Cat c" to represent the cat drawing.
  • Activity:
    • Write the following code on the board:
      • public static void main(String args[]){     Cat c = new Cat("sam", 9);     System.out.println(c);     foo(c);     System.out.println(c); } public void foo(Cat cat){     cat.moreLives(1); }
    • Introduce the class to your drawing that is representing "Cat c".
    • Ask for a volunteer to be the pointer to "Cat c", this student will point to the cat on the board and hold the paper that says "Cat c"
    • Ask for a volunteer to represent the "foo" method.
    • Now, have the student pointing to "Cat c" representation, give one "Cat c" card to the student representing the "foo" method.
      • Make sure the "Cat c" representative is still pointing to the cat on the board.
    • The "foo" method student will draw a new number of lives on the "Cat c" object.
    • Then have the student representing foo walk away.

More about this tip

External Source

Interview with Elissa Redmiles.