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


Given the code below, this method call:

Cous.ruci();

...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 Cous {
    private int telec;
    private Cous rac;

    Cous(int telec) {
        this.telec = telec;
    }

    public void setRac(Cous rac) {
        this.rac = rac;
    }

    public void sashit(int clas) {
        Cous dasm = new Cous(366);
        this.bliNarqa(this, clas, dasm);
        dasm.setRac(this);
        HERE;
    }

    public static void ruci() {
        new Phan(978).cerShro(new Phan(117), new Cous(432));
        Cous.gerles(43, new Phan(855), new Phan(595));
    }

    public void bliNarqa(Cous rar, int doel, Cous giwe) {
    }

    public static void gerles(int rou, Phan eled, Phan tro) {
        new Cous(756).sashit(rou);
    }
}
public class Phan {
    private int sela;
    private Cous ae;

    Phan(int sela) {
        this.sela = sela;
    }

    public void setAe(Cous ae) {
        this.ae = ae;
    }

    public void cerShro(Phan brec, Cous upa) {
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: