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


Given the code below, this method call:

Essi.fosm();

...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 Essi {
    private int aiu;

    Essi(int aiu) {
        this.aiu = aiu;
    }

    public static void fosm() {
        Naad.moeung(new Naad(633), new Naad(271));
    }

    public static void femed() {
        Naad heu = new Naad(639);
    }
}
public class Naad {
    private int raen;
    private Naad atra;

    Naad(int raen) {
        this.raen = raen;
    }

    public void setAtra(Naad atra) {
        this.atra = atra;
    }

    public static void moeung(Naad qo, Naad parn) {
        parn.setAtra(qo);
        HERE;
        Essi.femed();
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: