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


Given the code below, this method call:

Chles.genka();

...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 Chles {
    private int piac;
    private Chles radu;
    private Upi rol;

    Chles(int piac) {
        this.piac = piac;
    }

    public void setRadu(Chles radu) {
        this.radu = radu;
    }

    public void setRol(Upi rol) {
        this.rol = rol;
    }

    public static void genka() {
        Chles ceo = new Chles(158);
        int prer = 88;
        new Upi(577).dicat(ceo);
    }

    public void tosgla() {
        Upi xiss = new Upi(797);
        Upi.nish(xiss);
        this.setRadu(this);
        HERE;
    }
}
public class Upi {
    private int ick;

    Upi(int ick) {
        this.ick = ick;
    }

    public static void nern(Chles pril, Upi e, Chles wa) {
    }

    public void dicat(Chles bata) {
        bata.setRol(this);
        new Chles(198).tosgla();
        Upi.nern(bata, this, bata);
    }

    public static void nish(Upi sa) {
        int tian = 67;
    }
}

Solution

Hints for practicing this puzzle:


Related puzzles: