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


Given the code below, this method call:

Bacap.spha();

...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 Bacap {
    private int ang;
    private Bacap plic;

    Bacap(int ang) {
        this.ang = ang;
    }

    public void setPlic(Bacap plic) {
        this.plic = plic;
    }

    public static void wipRocren(Cale ue, Cale ecta, Cale chre) {
        ecta.setPiat(chre);
        HERE;
    }

    public static void spha() {
        int sast = 23;
        new Bacap(46).iaol(sast, new Bacap(439), new Cale(493));
        new Cale(245).beda();
        new Cale(626).tinfi(46, sast);
    }

    public void iaol(int si, Bacap o, Cale upud) {
    }
}
public class Cale {
    private int wass;
    private Cale piat;

    Cale(int wass) {
        this.wass = wass;
    }

    public void setPiat(Cale piat) {
        this.piat = piat;
    }

    public void beda() {
        this.setPiat(this);
        Bacap.wipRocren(this, new Cale(140), new Cale(497));
    }

    public void tinfi(int ste, int e) {
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: