What are OOP's Jargons and Complexities?

Xah Lee xah at xahlee.org
Wed May 25 02:42:21 EDT 2005


The Rise of “Static” versus “Instance” variables

In a normal programing language, variables inside functions are used by
the function, called local variables.

In OOP paradigm, as we've seen, super-subroutines (classes) are
assigned to variables (instantiation), and the inner-subroutines
(methods) are called thru the variables (objects). Because of this
mechanism, what's once known as local variables (class variables) can
now also be accessed thru the assigned variable (objet) by design. In
OOP parlance, this is to say that a class's variables can be accessed
thru the object reference, such as in myObject.data=4. For example:
mySurface = new a_surface();
mySurface.coordinatesList={...} // assign initial coordinates

However, sometimes a programmer only needs a collection of variables.
For exmple, a list of colors:
black = "#000000";
gray = "#808080";
green = "#008000";

In pure OOP, data as these now come with a subroutine (class) wrapper:
class listOfColors() {
  black = "#000000";
  gray = "#808080";
  green = "#008000";
}

Now to access these values, normally one needs to assign this
subroutine (class) to a variable (instantiation) as to create a object:
myColors = new listOfColors(); // instantiation! (creating a "object")
newColor = myColors.black;

As a workaround of this extraneous step is the birth of the concept of
“static” variables. (with the keyword “static” in Java) When a
variable is declared static, that variable can be accessed without
needing to instantiate its class. Example:
class listOfColors() {
  static black = "#000000";
  static gray = "#808080";
  static green = "#008000";
}
newColor = listOfColors.black;   // no instantiation required

The issue of staticality is also applicable to inner-subroutines
(methods). For example, if you are writing a collection of math
functions such as Sine, Cosine, Tangent... etc, you don't really want
to create a instance in order to use. Example:
class mathFunctions() {
  static sin (x) {...};         // a static method
  ...
}
print mathFunctions.sin(1);  // no need to create object before use


The non-static variant of variables and methods are called “instance
variables” or “instance methods”, or collectively “instance
members”. Note that static members and instance members are very
different. With static members, variables and methods can be called
without creating a object. But more subtly, for a static variable,
there is just one copy of the variable; for instance variables, each
object maintains its own copy of the variable. A class can declare just
some variables static. So, when multiple objects are created from the
class, some variables will share values while others having independent
copies. For example:
class a_surface() {
  static pi;                     // a static variable
  coordinatesList;               // a instance variable
  ...
};
a_surface.pi=3.1415926;          // assign value of pi for all
a_surface objects
mySurface1 = new a_surface();
mySurface1.coordinatesList={...} // assign coordinates to one a_surface
object
mySurface2 = new a_surface();
mySurface2.coordinatesList={...} // assign coordinates to another
a_surface object

The issues of static versus instance members, is one complexity arising
out of OOP.

----------
to be continued tomorrow.

This is part of an installment of the article
“What are OOP's Jargons and Complexities”
by Xah Lee, 20050128. The full text is at
 http://xahlee.org/Periodic_dosage_dir/t2/oop.html

© Copyright 2005 by Xah Lee. Verbatim duplication of the complete
article for non-profit purposes is granted.

The article is published in the following newsgroups:
comp.lang.c,comp.lang.c++,comp.lang.lisp,comp.unix.programmer
comp.lang.python,comp.lang.perl.misc,comp.lang.scheme,comp.lang.java.programmer
comp.lang.functional,comp.object,comp.software-eng,comp.software.patterns

 Xah
 xah at xahlee.orghttp://xahlee.org/




More information about the Python-list mailing list