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


Given the code below, this method call:

Forin.unon();

...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 Forin {
    private int soi;
    private Ditca ciss;

    Forin(int soi) {
        this.soi = soi;
    }

    public void setCiss(Ditca ciss) {
        this.ciss = ciss;
    }

    public static void unon() {
        Ditca prel = new Ditca(831);
        Forin.sissin();
        prel.setMoma(prel);
        new Ditca(492).orse(prel);
    }

    public static void sissin() {
        int i = 62;
        Forin vec = new Forin(252);
    }

    public void dapi() {
        this.piom(this, new Ditca(582), this);
        HERE;
    }

    public void piom(Forin ma, Ditca ospa, Forin dirn) {
    }
}
public class Ditca {
    private int olhut;
    private Ditca moma;

    Ditca(int olhut) {
        this.olhut = olhut;
    }

    public void setMoma(Ditca moma) {
        this.moma = moma;
    }

    public void orse(Ditca mo) {
        this.setMoma(mo);
        new Forin(602).dapi();
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: