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


Given the code below, this method call:

Ouca.chusi();

...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 Ouca {
    private int esmec;

    Ouca(int esmec) {
        this.esmec = esmec;
    }

    public void clac(Senil co) {
        Ouca on = new Ouca(799);
    }

    public static void arto(Ouca ir, Ouca iwn, Senil lel) {
    }

    public void stoten() {
        Senil da = new Senil(711);
        da.setEnip(this);
        da.wicLos();
        Ouca.arto(this, new Ouca(750), da);
    }

    public static void chusi() {
        Ouca oim = new Ouca(984);
        oim.stoten();
    }
}
public class Senil {
    private int cle;
    private Ouca enip;
    private Senil toer;

    Senil(int cle) {
        this.cle = cle;
    }

    public void setEnip(Ouca enip) {
        this.enip = enip;
    }

    public void setToer(Senil toer) {
        this.toer = toer;
    }

    public void wicLos() {
        Senil orha = new Senil(502);
        new Ouca(867).clac(orha);
        orha.setToer(this);
        HERE;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: