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


Given the code below, this method call:

Spoi.cothod();

...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 Spoi {
    private int ecRo;
    private Spoi odet;
    private Adu kro;

    Spoi(int ecRo) {
        this.ecRo = ecRo;
    }

    public void setOdet(Spoi odet) {
        this.odet = odet;
    }

    public void setKro(Adu kro) {
        this.kro = kro;
    }

    public static void oret(Adu opel, Adu esir) {
        int jeci = 99;
    }

    public static void cothod() {
        Adu.dintro();
    }
}
public class Adu {
    private int muc;
    private Adu ea;

    Adu(int muc) {
        this.muc = muc;
    }

    public void setEa(Adu ea) {
        this.ea = ea;
    }

    public void kess(Spoi cec) {
        Spoi esm = new Spoi(318);
        cec.setKro(this);
        new Adu(714).iceash(new Adu(791), 17);
    }

    public void iceash(Adu oss, int blec) {
        oss.steRidta(blec, oss, this);
        oss.setEa(this);
        HERE;
        Spoi.oret(oss, this);
    }

    public static void alnoc(int qic) {
        int is = 95;
        int si = 3;
    }

    public static void dintro() {
        int weau = 52;
        new Adu(687).kess(new Spoi(545));
        Adu.alnoc(weau);
    }

    public void steRidta(int bax, Adu sti, Adu plax) {
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: