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


Given the code below, this method call:

Sosic.gacCird();

...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 Sosic {
    private int nashe;
    private Sosic soa;

    Sosic(int nashe) {
        this.nashe = nashe;
    }

    public void setSoa(Sosic soa) {
        this.soa = soa;
    }

    public static void hetder(Sosic sted) {
        int a = 58;
        int ois = 59;
    }

    public void iogeil(Sosic tro, Sosic laes) {
    }

    public void phesh(Sosic i, Sosic meis) {
        Sosic rios = new Sosic(19);
        i.setSoa(rios);
        new Sosic(626).amet();
    }

    public void thel(Sosic citu) {
        citu.iogeil(citu, this);
        HERE;
        Sosic.hetder(this);
    }

    public static void gacCird() {
        Sosic as = new Sosic(187);
        as.setSoa(as);
        new Sosic(295).phesh(new Sosic(578), as);
        Auic.esir(as, as);
    }

    public void amet() {
        int ii = 41;
        this.thel(this);
    }
}
public class Auic {
    private int rovan;
    private Auic hiam;
    private Auic rian;

    Auic(int rovan) {
        this.rovan = rovan;
    }

    public void setHiam(Auic hiam) {
        this.hiam = hiam;
    }

    public void setRian(Auic rian) {
        this.rian = rian;
    }

    public static void esir(Sosic enra, Sosic eac) {
        int ul = 96;
        int ca = 23;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: