Misconception: students struggle with scopes of variables and may not understand when a variable can be accessed and when it cannot be accessed.

  • To avoid confusion about the "lifetime" of values and objects, remind students that values and objects must be explicitly assigned to variables if they are to be used later.
    • Students have the misconception that returning values or objects is the same as storing them.
    • Students may also believe that simply typing the value or calling the constructor without assigning it to a variable “saves” the value or object for later use.
    • These values and objects cease to exist once they are returned or created without being assigning to a variable.
    • Therefore, it’s important to remind students that values and objects that are returned but not assigned to variables, or are created without assignment, are conceptually in limbo where they cannot ever be used again.
  • Students struggle with scope and may not understand when a variable needs to be used outside of a single method
    • Students will want to use a local variable inside and outside a method, which can result in multiple variables with the same name.
    • Null pointer exceptions can be a result of mistakenly having multiple variables with the same name.
    • Avoid this problem by having a rule against repeating names in intro classes.

More about this tip

External Source

Interview with Dani McAvoy

"Visual program simulation in introductory programming education" by Juha Sorva, from Appendix A: Misconception Catalogue