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


Given the code below, this method call:

Brus.chesos();

...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 Brus {
    private int idsha;
    private Aiss ahe;

    Brus(int idsha) {
        this.idsha = idsha;
    }

    public void setAhe(Aiss ahe) {
        this.ahe = ahe;
    }

    public void qepAsnem(Brus fa) {
        Aiss ta = new Aiss(456);
        fa.setAhe(ta);
        HERE;
        Aiss.grest(new Brus(800));
    }

    public void setung(Brus trea, int joal, int ro) {
        trea.qepAsnem(this);
        new Aiss(274).propta(ro, this);
    }

    public static void chesos() {
        new Brus(365).setung(new Brus(137), 94, 76);
    }
}
public class Aiss {
    private int tia;
    private Aiss meul;

    Aiss(int tia) {
        this.tia = tia;
    }

    public void setMeul(Aiss meul) {
        this.meul = meul;
    }

    public void propta(int ecae, Brus se) {
    }

    public static void grest(Brus le) {
        Brus peo = new Brus(546);
        Aiss whe = new Aiss(805);
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: