ReferenceLabeledWithDynamicType
DRAFT

Misconception:

In a stack and heap diagram, every variable is labeled with its name and its type. In case of reference variables, the type we have to label the variable with is the type of the object it currently points to.

Incorrect

In stack and heap diagrams, reference variables are labeled with the types of the objects they refer to

Correct

Correction
Here is what's right.

Every variable in a stack and heap diagram is labeled with its “static type” (i.e., the type it is declared with). The “dynamic type” (i.e., the type of the object the variable refers to) can change throughout the execution (because we can assign values to the variable, and those values can be of any subtype of the variable’s “static type”).

For reference variables, the “static type” and “dynamic type” can differ. E.g., in Sprite s = new Pacman(), the “static type” of variable s is Sprite, but the “dynamic type” of variable s after the statement completes is Pacman, because the variable will point to a Pacman object.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.