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


Given the code below, this method call:

Musm.oisel();

...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 Musm {
    private int fiHul;

    Musm(int fiHul) {
        this.fiHul = fiHul;
    }

    public static void oisel() {
        Musm.xinil(new Useng(814));
        new Musm(138).entPetsud(new Useng(338));
    }

    public void entPetsud(Useng ba) {
        Musm aent = new Musm(560);
        ba.setFe(this);
        Musm.sceken(aent);
        Musm.iseod(this, aent, ba);
    }

    public static void iseod(Musm ec, Musm hu, Useng at) {
    }

    public static void sceken(Musm ooss) {
        Useng asen = new Useng(264);
        asen.setFe(ooss);
        HERE;
    }

    public static void xinil(Useng to) {
        Useng usha = new Useng(646);
        Useng mith = new Useng(975);
    }
}
public class Useng {
    private int menk;
    private Musm fe;
    private Useng loea;

    Useng(int menk) {
        this.menk = menk;
    }

    public void setFe(Musm fe) {
        this.fe = fe;
    }

    public void setLoea(Useng loea) {
        this.loea = loea;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: