In
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
s, name binding is the association of entities (data and/or code) with
identifier
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, physical countable object (or class thereof), or physical noncountable ...
s.
An identifier bound to an
object
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an ...
is said to
reference
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 ''name'' ...
that object.
Machine language
In computer programming, machine code is any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). Each instruction causes the CPU to perform a very ...
s 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
In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
Functions may ...
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
In computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented ...
, as in a
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
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 (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
code:
public void foo(java.util.List list)
List
is an
interface
Interface or interfacing may refer to:
Academic journals
* ''Interface'' (journal), by the Electrochemical Society
* ''Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics''
* '' Inte ...
, so
list
must refer to a
subtype
Subtype may refer to:
* Viral subtypes, such as Subtypes of HIV
* Subtyping
In programming language theory, subtyping (also subtype polymorphism or inclusion polymorphism) is a form of type polymorphism in which a subtype is a datatype that is ...
of it.
list
may reference a
LinkedList
, an
ArrayList
, or some other
subtype
Subtype may refer to:
* Viral subtypes, such as Subtypes of HIV
* Subtyping
In programming language theory, subtyping (also subtype polymorphism or inclusion polymorphism) is a form of type polymorphism in which a subtype is a datatype that is ...
of
List
. The method referenced by
add
is not known until runtime. In C, such an instance of dynamic binding may be a call to a function pointed to by a variable or expression of a
function pointer
A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory. Dereferencing the function poin ...
type whose value is unknown until it is evaluated at run-time.
Rebinding and mutation
Rebinding should not be confused with mutation.
* ''Rebinding'' is a change to the ''referencing'' identifier.
* ''Mutation'' is a change to the ''referenced'' entity.
Consider the following
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
code:
LinkedList list;
list = new LinkedList();
list.add("foo");
list = null;
The identifier
list
initially references nothing (it is
uninitialized); it is then rebound to reference an object (a linked list of strings). The linked list referenced by
list
is then mutated, adding a string to the list. Lastly,
list
is rebound to
null
.
Late static
Late static binding is a variant of binding somewhere between static and dynamic binding. Consider the following
PHP
PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group ...
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
*
Late binding
In computing, late binding or dynamic linkage—though not an identical process to Dynamic linker, dynamically linking imported code Library (computing), libraries—is a computer programming mechanism in which the Method (computer programming), ...
*
Branch table
In computer programming, a branch table or jump table is a method of transferring program control (Branch (computer science), branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of b ...
method of applying name binding via branch table or function pointers
*
Dynamic dispatch
In computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented ...
*
Higher-order abstract syntax
In computer science, higher-order abstract syntax (abbreviated HOAS) is a technique for the representation of abstract syntax trees for languages with variable binders.
Relation to first-order abstract syntax
An abstract syntax is ''abstract'' be ...
(HOAS)
References
{{reflist
Programming language concepts
Articles with example Java code
Definition
ja:束縛 (情報工学)
pt:Vinculação de nomes (computação)