{{short description |When a binding occurs in software}}

In computer programming, '''binding time''' describes when an association is made in software between two data or code entities. This may occur either before or after execution starts. Early (a.k.a. static) binding occurs before the program starts running and cannot change during runtime. Late (a.k.a. dynamic or virtual) binding occurs as the program is running.<ref name=ieee24765:2010(E)>{{Citation |title=Systems and software engineering — Vocabulary ISO/IEC/IEEE 24765:2010(E)|publisher=IEEE |date=Dec 15, 2010}}</ref> Binding time applies to any type of binding including name, memory (i.e. via malloc), and type (i.e. for literals).

==Examples== The following Java code demonstrates both binding times. The method {{code |foo}} is early-bound to the code block that follows the function declaration on line 3. The call to {{code |add}} is late-bound since <code>List</code> is an interface, so <code>list</code> must refer to a subtype of it. <code>list</code> may reference a <code>LinkedList</code>, an <code>ArrayList</code>, or some other subtype of <code>List</code>. The method referenced by <code>add</code> is not known until runtime.

:<syntaxhighlight lang="java" line> import java.util.List;

public void foo(List<String> list) { list.add("bar"); } </syntaxhighlight>

==Related==

===Late static=== Late static binding is a variant of binding somewhere between static and dynamic binding. Consider the following PHP example: <syntaxhighlight lang="php"> class A { public static $word = "hello"; public static function hello() { print self::$word; } }

class B extends A { public static $word = "bye"; }

B::hello(); </syntaxhighlight>

In this example, the PHP interpreter binds the keyword <code>self</code> inside <code>A::hello()</code> to class <code>A</code>, and so the call to <code>B::hello()</code> produces the string "hello". If the semantics of <code>self::$word</code> 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.<ref>{{cite web|url=http://us2.php.net/manual/en/language.oop5.late-static-bindings.php|title=Late Static Bindings|access-date=July 3, 2013}}</ref> Specifically, if <code>self::$word</code> in the above were changed to <code>static::$word</code> as shown in the following block, where the keyword <code>static</code> would only be bound at runtime, then the result of the call to <code>B::hello()</code> would be "bye":

<syntaxhighlight lang="php"> class A { public static $word = "hello"; public static function hello() { print static::$word; } }

class B extends A { public static $word = "bye"; }

B::hello(); </syntaxhighlight>

==See also== * {{Annotated link |Program lifecycle phase}}

==References== {{reflist}}

Category:Programming language concepts Category:Articles with example Java code