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


Given the code below, this method call:

Nanli.pirdio();

...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 Nanli {
    private int ucBapil;
    private Escoc xuc;

    Nanli(int ucBapil) {
        this.ucBapil = ucBapil;
    }

    public void setXuc(Escoc xuc) {
        this.xuc = xuc;
    }

    public static void pirdio() {
        int tre = 3;
        Nanli sefi = new Nanli(528);
        sefi.zodent(sefi, tre, new Nanli(787));
        sefi.cewi();
    }

    public void cewi() {
        new Escoc(27).tolwa();
    }

    public void zodent(Nanli hi, int trir, Nanli nust) {
    }

    public static void dodsac(Escoc epuo, Escoc ossu) {
        int an = 76;
    }
}
public class Escoc {
    private int tuss;
    private Escoc ri;

    Escoc(int tuss) {
        this.tuss = tuss;
    }

    public void setRi(Escoc ri) {
        this.ri = ri;
    }

    public void tolwa() {
        Escoc i = new Escoc(224);
        Escoc ost = new Escoc(948);
        Nanli.dodsac(i, this);
        this.setRi(i);
        HERE;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: