ObjectLabeledWithConstructorSignature
DRAFT

Misconception:

When drawing an object in the heap of a stack and heap diagram, the label of the object must be the method signature of the constructor call that created the object (e.g., Pacman()).

Incorrect

In stack and heap diagrams, an object on the heap is labeled like a stack frame of a constructor call

Correct

Correction
Here is what's right.

Not really.

An object and a constructor’s stack frame are two separate areas of memory used for two separate purposes. They are also labeled differently.

An object on the heap holds the state of the object (the instance variables of the object). In a stack and heap diagram we write the object’s class name next to the object.

A stack frame of a constructor holds the local variables and the parameters used while a constructor is executing. A constructor call is a special kind of method call. A constructor’s stack frame is like a normal method’s stack frame. In a stack and heap diagram we write the constructor’s signature (its name and parameters) next to its stack frame.

The lifetime of an object is potentially much longer than the lifetime of a constructor call. The memory holding an object on the heap is reclaimed when no more reference points to it. The memory holding a constructor’s stack frame is reclaimed when the constructor returns.

Language

Java
Notional Machine
StackHeapGlobalDiagram

Concepts

Expressible In

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.