This wikipedia states:
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(java.util.List<String> list) { list.add("bar"); }List is an interface, so list must refer to a subtype of it. Is it a reference to a LinkedList, an ArrayList, or some other subtype of List? The actual method referenced by add is not known until runtime.
Consider this example:
List<String> list;
list = new LinkedList<String>();
foo(list);
list = new ArrayList<String>();
foo(list);
Why is the actual method referenced here is not know until runtime? Couldn't the compiler just check for each call of foo of which type the object list is assigned to? Of course this would be only possible if the program is deterministic and no randomness is involved (e.g. user interaction).
Is this what (in general) in the quoted statement is about or is my understanding wrong?
In the special case when the program is deterministic, is static binding used or is - in Java - always dynamic binding used, regardless of what is possible? If so, why?