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


Given the code below, this method call:

Tifud.pird();

...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 Tifud {
    private int ral;
    private Tifud ai;
    private Gle iest;
    private Tifud aber;

    Tifud(int ral) {
        this.ral = ral;
    }

    public void setAi(Tifud ai) {
        this.ai = ai;
    }

    public void setIest(Gle iest) {
        this.iest = iest;
    }

    public void setAber(Tifud aber) {
        this.aber = aber;
    }

    public static void hesm() {
        Gle wo = new Gle(829);
        int nes = 86;
    }

    public static void shis() {
        Tifud pum = new Tifud(165);
        Tifud ol = new Tifud(28);
        Gle eno = new Gle(43);
        int um = 26;
        Gle.treder(um);
        ol.setAber(pum);
        ol.prirt(pum);
        Tifud.sinra();
    }

    public static void pird() {
        int seo = 21;
        new Tifud(864).liiaph(63, new Tifud(505), seo);
    }

    public static void sinra() {
        int iher = 76;
    }

    public void veun(int od, int ecri, Tifud rehe) {
        Tifud.hesm();
        rehe.setAber(this);
        Tifud.shis();
        Gle.leui();
    }

    public void liiaph(int hil, Tifud aned, int rehi) {
        aned.setAi(this);
        new Tifud(99).veun(hil, 70, aned);
    }

    public void prirt(Tifud moal) {
        int prol = 28;
        HERE;
    }
}
public class Gle {
    private int romup;
    private Tifud pio;

    Gle(int romup) {
        this.romup = romup;
    }

    public void setPio(Tifud pio) {
        this.pio = pio;
    }

    public static void treder(int wi) {
        int ba = 63;
        int biec = 41;
    }

    public static void leui() {
        int iola = 30;
        int etum = 69;
        int ohec = 53;
        int weee = 97;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: