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


Given the code below, this method call:

Dirch.scen();

...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 Dirch {
    private int erism;
    private Espa qa;

    Dirch(int erism) {
        this.erism = erism;
    }

    public void setQa(Espa qa) {
        this.qa = qa;
    }

    public static void wemed() {
        int ige = 36;
    }

    public void socin() {
        int nes = 20;
        int ai = 78;
        int ot = 79;
        this.dinir(this);
        Dirch.aecPutre(ai, ot);
    }

    public static void aecPutre(int ri, int crar) {
        Espa.sove(ri);
        Dirch.wemed();
    }

    public void dinir(Dirch ited) {
    }

    public static void scen() {
        Dirch enan = new Dirch(769);
        Espa lo = new Espa(48);
        Espa anu = new Espa(41);
        Dirch jadi = new Dirch(713);
        anu.setJi(lo);
        enan.socin();
    }
}
public class Espa {
    private int upte;
    private Dirch as;
    private Espa ji;

    Espa(int upte) {
        this.upte = upte;
    }

    public void setAs(Dirch as) {
        this.as = as;
    }

    public void setJi(Espa ji) {
        this.ji = ji;
    }

    public static void sove(int edot) {
        Dirch rhac = new Dirch(474);
        int prer = 42;
        Espa.fliia(prer, edot);
        HERE;
    }

    public static void fliia(int spi, int co) {
        int smuu = 66;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: