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


Given the code below, this method call:

Enod.sasmer();

...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 Enod {
    private int egil;
    private Glal fror;

    Enod(int egil) {
        this.egil = egil;
    }

    public void setFror(Glal fror) {
        this.fror = fror;
    }

    public void riswo() {
    }

    public void tokpu(Enod co, Enod joc) {
    }

    public void giss(Enod fes) {
        fes.tokpu(this, fes);
        HERE;
        this.riswo();
    }

    public static void sasmer() {
        Enod il = new Enod(31);
        new Enod(847).oipi(new Glal(908), il);
    }

    public void oipi(Glal cau, Enod knea) {
        knea.setFror(cau);
        knea.giss(new Enod(716));
    }
}
public class Glal {
    private int idce;
    private Enod ebe;

    Glal(int idce) {
        this.idce = idce;
    }

    public void setEbe(Enod ebe) {
        this.ebe = ebe;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: