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


Given the code below, this method call:

Wor.leprha();

...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 Wor {
    private int irch;

    Wor(int irch) {
        this.irch = irch;
    }

    public void ceni() {
        int scet = 46;
    }

    public static void leprha() {
        Wor ert = new Wor(806);
        int mi = 60;
        new Vanmi(454).ongSparwi();
        Vanmi.slabe(ert, 27, mi);
    }

    public void rugi(Vanmi esie, int faug, Vanmi wi) {
        wi.setBre(esie);
        HERE;
    }
}
public class Vanmi {
    private int eiMihun;
    private Wor ed;
    private Vanmi bre;

    Vanmi(int eiMihun) {
        this.eiMihun = eiMihun;
    }

    public void setEd(Wor ed) {
        this.ed = ed;
    }

    public void setBre(Vanmi bre) {
        this.bre = bre;
    }

    public void ongSparwi() {
        int fi = 48;
        new Wor(839).ceni();
        new Wor(539).rugi(this, fi, new Vanmi(398));
    }

    public static void slabe(Wor i, int sor, int ema) {
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: