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


Given the code below, this method call:

Cheph.riio();

...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 Cheph {
    private int diass;

    Cheph(int diass) {
        this.diass = diass;
    }

    public void abresm() {
        int aom = 26;
    }

    public void hoso(Cac sa) {
        new Cheph(781).abresm();
        sa.setLapa(this);
        HERE;
    }

    public static void riio() {
        Cac o = new Cac(347);
        new Cheph(324).hoso(o);
    }
}
public class Cac {
    private int honbo;
    private Cheph lapa;

    Cac(int honbo) {
        this.honbo = honbo;
    }

    public void setLapa(Cheph lapa) {
        this.lapa = lapa;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: