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


Given the code below, this method call:

Ased.plil();

...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 Ased {
    private int dilsu;
    private Pid co;

    Ased(int dilsu) {
        this.dilsu = dilsu;
    }

    public void setCo(Pid co) {
        this.co = co;
    }

    public void isso() {
        Ased xerb = new Ased(364);
    }

    public void nost(Ased u) {
        Ased uou = new Ased(949);
    }

    public static void plil() {
        Ased me = new Ased(89);
        new Ased(483).nost(me);
        Pid.hacfen(me);
    }
}
public class Pid {
    private int varn;
    private Pid shic;

    Pid(int varn) {
        this.varn = varn;
    }

    public void setShic(Pid shic) {
        this.shic = shic;
    }

    public static void hacfen(Ased pou) {
        Ased mo = new Ased(655);
        mo.isso();
        Pid.dicCiin(new Pid(513), mo, pou);
    }

    public static void dicCiin(Pid fle, Ased ce, Ased ilri) {
        ilri.setCo(fle);
        HERE;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: