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


Given the code below, this method call:

Sclu.rhie();

...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 Sclu {
    private int alspu;
    private Vaios prir;
    private Sclu adsu;
    private Sclu toar;

    Sclu(int alspu) {
        this.alspu = alspu;
    }

    public void setPrir(Vaios prir) {
        this.prir = prir;
    }

    public void setAdsu(Sclu adsu) {
        this.adsu = adsu;
    }

    public void setToar(Sclu toar) {
        this.toar = toar;
    }

    public static void rhie() {
        Sclu.benpi(new Sclu(226), 72);
        Sclu.desten(new Vaios(117));
    }

    public static void lesde() {
        int mi = 89;
        HERE;
    }

    public static void benpi(Sclu ca, int crir) {
        Sclu er = new Sclu(838);
    }

    public static void desten(Vaios i) {
        Sclu lo = new Sclu(917);
        i.danbel(i);
        lo.setPrir(i);
        Vaios.cemQecka();
        i.erdte(i);
    }
}
public class Vaios {
    private int saUl;

    Vaios(int saUl) {
        this.saUl = saUl;
    }

    public void erdte(Vaios fus) {
    }

    public void danbel(Vaios iar) {
        Vaios doce = new Vaios(480);
    }

    public static void cemQecka() {
        Vaios ad = new Vaios(118);
        Sclu io = new Sclu(779);
        Sclu de = new Sclu(33);
        io.setAdsu(de);
        Sclu.lesde();
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: