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


Given the code below, this method call:

Pec.febu();

...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 Pec {
    private int iass;

    Pec(int iass) {
        this.iass = iass;
    }

    public static void febu() {
        Pec slis = new Pec(742);
        Riar.mepon(new Riar(335), slis);
    }

    public void spla(Riar piac) {
    }
}
public class Riar {
    private int ufria;
    private Pec ifri;

    Riar(int ufria) {
        this.ufria = ufria;
    }

    public void setIfri(Pec ifri) {
        this.ifri = ifri;
    }

    public static void mepon(Riar sor, Pec ce) {
        sor.setIfri(ce);
        HERE;
        ce.spla(new Riar(969));
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: