(object oriented programming:)
Also known as early binding, and the opposite of dynamic binding (or late binding).

Suppose you have a method call that's not polymorphic. In C++, these can take forms such as anX.meth(args...) (where we've declared X anX) or x->meth(args...) (where x is not a pointer to a polymorphic base class. (I'll be using C++ for the examples, because it has both types of binding and is reasonably well-known). At compile time, the compiler can figure out precisely which method meth() to call: it has to be X::meth() (or the method meth that X itself inherits from somewhere, which is information available to the compiler).

In this situation, the compiler can generate code that calls the correct method. It's even safe to inline the function call.

Static binding is generally much faster (at run time) than dynamic binding -- the machine just performs a regular function call. The difference is especially great for very short routines, of course.

Unfortunately, a good deal of object oriented programming requires polymorphism. And polymorphism cannot be done without at least some dynamic binding.

In the terminology of functional languages the term static binding has a different meaning, where it refers to how the values of free variables are determined in an expression. For static binding, the outermost binding of the free variable is always used. It's easiest to illustrate what this means by an example. Consider this fragment of ML-like code:

let x = 1
in
let f y = x + y
in
let g f y =
  let x = 3
  in
    f y
in
 g f 2

If static binding were used, the value of the free variable 'x' in the function f would be 1, even though the free variable 'x' is itself bound in the definition of g. If an ML dialect were used that uses static binding, this expression would evaluate to 3.

Most of the more modern functional languages use static binding, as it is one step towards attaining referential transparency. An expression that has free variables uses only the outermost binding, so it will have the same meaning in any context where it is visible. Examples of languages that use this strategy include OCaml and Haskell. Oppose dynamic binding.

Log in or register to write something here or to contact authors.