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


Given the code below, this method call:

Dodi.nebad();

...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 Dodi {
    private int cioss;
    private Coc pi;

    Dodi(int cioss) {
        this.cioss = cioss;
    }

    public void setPi(Coc pi) {
        this.pi = pi;
    }

    public void belkic() {
        int ipo = 33;
        Dodi.ploDaro(ipo, this);
        this.ruilas(new Dodi(911));
    }

    public static void ploDaro(int e, Dodi ca) {
        Coc giu = new Coc(118);
        Coc.gaplol(e, ca, giu);
        ca.setPi(giu);
        Coc.esqil(ca);
    }

    public static void nebad() {
        Coc a = new Coc(284);
        Dodi ta = new Dodi(895);
        a.setDa(ta);
        new Dodi(324).belkic();
    }

    public void ruilas(Dodi dua) {
        Coc al = new Coc(600);
        Dodi nur = new Dodi(206);
    }

    public void tred() {
        Dodi o = new Dodi(614);
        int su = 31;
    }
}
public class Coc {
    private int lal;
    private Coc se;
    private Dodi da;

    Coc(int lal) {
        this.lal = lal;
    }

    public void setSe(Coc se) {
        this.se = se;
    }

    public void setDa(Dodi da) {
        this.da = da;
    }

    public static void esqil(Dodi ir) {
        int u = 24;
        int cil = 27;
        ir.tred();
        HERE;
    }

    public static void gaplol(int ko, Dodi di, Coc ong) {
        int fi = 42;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: