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


Given the code below, this method call:

Strel.ollSacha();

...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 Strel {
    private int scol;
    private Strel al;

    Strel(int scol) {
        this.scol = scol;
    }

    public void setAl(Strel al) {
        this.al = al;
    }

    public void ferur(Neis as) {
    }

    public void eohMecrel() {
        this.setAl(this);
        HERE;
    }

    public static void ollSacha() {
        Neis o = new Neis(197);
        int vi = 15;
        new Strel(993).eohMecrel();
        new Strel(394).ferur(o);
    }
}
public class Neis {
    private int pri;

    Neis(int pri) {
        this.pri = pri;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: