Name binding
   HOME

TheInfoList



OR:

In
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s, name binding is the association of entities (data and/or code) with identifiers. An identifier bound to an object is said to
reference A reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a ''nam ...
that object. Machine languages have no built-in notion of identifiers, but name-object bindings as a service and notation for the programmer is implemented by programming languages. Binding is intimately connected with scoping, as scope determines which names bind to which objects – at which locations in the program code ( lexically) and in which one of the possible execution paths ( temporally). Use of an identifier in a context that establishes a binding for is called a binding (or defining) occurrence. In all other occurrences (e.g., in expressions, assignments, and subprogram calls), an identifier stands for what it is bound to; such occurrences are called applied occurrences.


Binding time

* ''Static binding'' (or ''early binding'') is name binding performed before the program is run. * ''Dynamic binding'' (or '' late binding'' or ''virtual binding'') is name binding performed as the program is running. 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 Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
code: public void foo(java.util.List list) List is an interface, so list must refer to a subtype of it. list may reference a LinkedList, an ArrayList, or some other subtype of List. The method referenced by add is not known until runtime. In C, which does not have dynamic binding, a similar goal may be achieved by a call to a function pointed to by a variable or expression of a function pointer type, whose value is unknown until it is evaluated at run-time.


Rebinding and mutation

Rebinding should not be confused with mutation or assignment. * ''Rebinding'' is a change to the ''referencing'' identifier. * ''Assignment'' is a change to (the referenced) variable. * ''Mutation'' is a change to an object in memory, possibly referenced by a variable or bound to an identifier. Consider the following
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
code: LinkedList list; list = new LinkedList(); list.add("foo"); list = null; The identifier list is bound to a variable in the first line; in the second, an object (a linked list of strings) is assigned to the variable. The linked list referenced by the variable is then mutated, adding a string to the list. Next, the variable is assigned the constant null. In the last line, the identifier is rebound for the scope of the block. Operations within the block access a new variable and not the variable previously bound to list.


Late static

Late static binding is a variant of binding somewhere between static and dynamic binding. Consider the following PHP example: class A class B extends A B::hello(); In this example, the PHP interpreter binds the keyword self inside A::hello() to class A, and so the call to B::hello() produces the string "hello". If the semantics of self::$word had been based on late static binding, then the result would have been "bye". Beginning with PHP version 5.3, late static binding is supported. Specifically, if self::$word in the above were changed to static::$word as shown in the following block, where the keyword static would only be bound at runtime, then the result of the call to B::hello() would be "bye": class A class B extends A B::hello();


See also

* *


References

{{reflist Programming language concepts Articles with example Java code Definition ja:束縛 (情報工学) pt:Vinculação de nomes (computação)