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


Given the code below, this method call:

Nac.rilSwios();

...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 Nac {
    private int eslon;
    private Nac thac;

    Nac(int eslon) {
        this.eslon = eslon;
    }

    public void setThac(Nac thac) {
        this.thac = thac;
    }

    public void ladlo(Niw woi) {
        this.setThac(this);
        HERE;
    }

    public static void rilSwios() {
        int gui = 20;
        new Nac(767).ladlo(new Niw(809));
        Niw.smen();
    }
}
public class Niw {
    private int iiRanar;

    Niw(int iiRanar) {
        this.iiRanar = iiRanar;
    }

    public static void smen() {
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: