Source Code. Projects. Nerd Stuff. Art Stuff.

Lesson 11: Static vs. Instance Variables and Functions

Summary of Static vs. Instance Variables and Functions

4 Kinds of Variables/Constants: Instance (non-static members), Static Members, Local, and Global

Instance Variable (Member Variable)

Static Member Variable (Class Variable)

Parameter or Local Variable

Global Variable

  • Declared in a class
  • Declared in a class as “static”
  • Declared in a function
  • Declared outside all functions and classes
  • Created when an object is instantiated
  • Created when program begins to execute
  • Created when function is called
  • Created when program begins to execute
  • Retains value as long as object exists
  • Retains value for entire program
  • Retains value until function returns
  • Retains value for entire program
  • Access controlled by “public” and “private”
  • Access controlled by “public” and “private”
  • Access limited to function in which it’s declared
  • Unlimited access
  • Can only be referenced as part of an object, e.g. objectName.variableName or, if referenced from within a method (non-static member function), simply variableName
  • Must be referenced as part of a class, e.g. className::variableName or, if called from within a member function, simply variableName
  • Referenced without any qualification (not part of any other structure)
  • Referenced without any qualification (not part of any other structure)
  • Stored inside an object’s memory on the stack or heap
  • Stored on the heap with the class’s info that can be referenced from anywhere
  • Stored inside a function’s memory on the stack
  • Stored on the heap so it can be referenced from anywhere

3 Kinds of Functions: Stand-Alone Functions, Static Member Functions, and Methods (non-static member functions)

Stand-Alone Function

Static Member Function

Member Function

(also called an “Instance Method”)

  • Declared outside all other definitions
  • Declared in a class as “static”
  • Declared in a class
  • Available once program begins to execute
  • Available once program begins to execute
  • Available once object is instantiated (constructed)
  • Unlimited access
  • Access controlled by “public” and “private”
  • Access controlled by “public” and “private”
  • Called in isolation (with no context)
  • Must be called as part of class, e.g. className::functionName(…); or, if called from within another member function, simply functionName(…);
  • Must be called on an object, e.g. objectName.functionName(…); or, if called from within another instance method, simply functionName(…);
  • All parameters are explicit
  • Implicit parameter is the class it’s defined in
  • Implicit parameter is the object it’s called on (pointer this used to explicitly refer to this object)
  • Can only reference global variables, plus local variables and parameters defined by function. (member variables can be referenced through classes / object variables)
  • Can only reference global and static variables, plus local variables and parameters defined by function. (Instance variables can be referenced through object variables)
  • Can reference both instance and static variables, plus local variables and parameters defined by function, and global variables