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


Given the code below, this method call:

Duter.stac();

...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 Duter {
    private int foc;
    private Duter thi;

    Duter(int foc) {
        this.foc = foc;
    }

    public void setThi(Duter thi) {
        this.thi = thi;
    }

    public void arang(Hotil o) {
        int redi = 45;
    }

    public static void stac() {
        new Hotil(948).feric(74, new Duter(907));
    }
}
public class Hotil {
    private int rhun;
    private Duter uac;

    Hotil(int rhun) {
        this.rhun = rhun;
    }

    public void setUac(Duter uac) {
        this.uac = uac;
    }

    public void sproi(Hotil a) {
        int umel = 56;
    }

    public void thrinn() {
        int ro = 10;
        Hotil eal = new Hotil(731);
        eal.sproi(eal);
        HERE;
    }

    public void feric(int en, Duter juli) {
        juli.setThi(juli);
        new Hotil(315).thrinn();
        juli.arang(this);
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: