Lifetime of a Variable

  • A Variable has a storage class which sets its lifetime

    • Local variables are local to
      a block
      - allocated at entry to
      a block & discarded at exit

    • Instance variables are local
      to an object
      - allocated when
      object instantiated & discarded
      when it is garbage collected

    • Class (static) variables are
      local to a class - allocated when a class is loaded
      and discarded when it is unloaded

  • An object (referred to by a variable) is garbage
    collected when there are no references to it