Name binding
From Free net encyclopedia
In programming languages, name binding refers to the association of values with identifiers. An identifier bound to a value is said to reference that value. Since computers themselves have no notion of identifiers, there is no binding at the machine language level — name binding is an abstraction provided by programming languages. Binding is intimately connected with scoping, as scope determines when binding occurs.
Rebinding and mutation
Rebinding should not be confused with mutation — "rebinding" is changes to the referencing identifier; "mutation" is changes to the referenced value. Consider the following Java code:
LinkedList<String> l; l = new LinkedList(); l.add("foo"); l = null;
The identifier l
at first references nothing (the null
object); it is then rebound to reference an object (a linked list of strings). The linked list referenced by l
is then mutated, adding a string to the list. Lastly, l
is rebound to the null
object.
Binding time
The binding of names before the program is run is called static (also "early"); bindings performed as the program runs are dynamic (also "late" or "virtual").
An example of a static binding is a direct C function call: the function referenced by the identifier cannot change at runtime. An example of dynamic binding is dynamic dispatch, as in a [[C++]] virtual method call. Since the specific type of a polymorphic object is not known before runtime (in general), the executed function is dynamically bound. Take, for example, the following Java code:
public void foo(List<String> l) { l.add("foo"); }
Is l
a LinkedList
, an ArrayList
, or some other subtype? The actual method referenced by add
is not known until runtime. (Note that in the first example, the actual method is known.)
Since compiled programs are often relocatable in memory, every memory reference is ultimately a dynamic binding. Each variable or function is referenced as an offset from a memory segment, which is not known until runtime. This is a pedantic distinction, however, as binding operates at the programming-language level and not the machine level.de:Dynamische Bindung