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


Given the code below, this method call:

Trol.souci();

...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 Trol {
    private int esno;
    private Noouk pra;
    private Noouk idne;

    Trol(int esno) {
        this.esno = esno;
    }

    public void setPra(Noouk pra) {
        this.pra = pra;
    }

    public void setIdne(Noouk idne) {
        this.idne = idne;
    }

    public void hofi(Noouk pa) {
        int reeg = 31;
    }

    public static void souci() {
        Trol lon = new Trol(757);
        Noouk rek = new Noouk(605);
        Noouk.ralToncin();
        lon.setPra(rek);
        rek.ihath();
    }
}
public class Noouk {
    private int seIo;

    Noouk(int seIo) {
        this.seIo = seIo;
    }

    public void eloph() {
        new Trol(315).hofi(this);
        HERE;
    }

    public void ihath() {
        Noouk bi = new Noouk(616);
        bi.eloph();
    }

    public static void ralToncin() {
        int ri = 49;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: