Stack frames and objects (like the Idea Lab activity): Correct Solution


Given the code below, this method call:

Gir.lefba();

...will eventually reach the point marked HERE. Draw a diagram of the stack and the heap at that point.

(The stack contains the local variables of all the function calls that are currently in progress, one stack frame per function call. The heap contains all of the objects that currently exist.)

In your diagram:

The code:

public class Gir {
    private int cahic;

    Gir(int cahic) {
        this.cahic = cahic;
    }

    public void letgau(Gir pa, Gir eant) {
        pa.pasm(pa);
        HERE;
    }

    public void pasm(Gir nasm) {
    }

    public void holoss(Gir soos) {
    }

    public static void lefba() {
        int wo = 51;
        Gir o = new Gir(654);
        new Gir(677).berc(o, 75, wo);
        new Gir(683).holoss(o);
    }

    public void berc(Gir pso, int he, int tric) {
        this.letgau(pso, new Gir(511));
    }
}
public class Gna {
    private int hino;
    private Gir nem;
    private Gna trer;

    Gna(int hino) {
        this.hino = hino;
    }

    public void setNem(Gir nem) {
        this.nem = nem;
    }

    public void setTrer(Gna trer) {
        this.trer = trer;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: