When you call a method in Java, it is resolved either at compile time or at runtime, depending upon whether its a virtual method or a static method. When a method call is resolved at compile time, it is known as
static binding, while if method invocation is resolved at runtime, it is known as
Dynamic binding or Late binding. Since Java is an object oriented programming language and by virtue of that it supports
Polymorphism. Because of polymorphism, a reference variable of type
Parent can hold an object of type
Child, which extends Parent. Now if you call a virtual method (not private, final or static) on this object, then Compiler can not find actual method, because it could be the one, which is defined in
Parent class, or the one which
Child has overridden. This call can only be resolved at runtime, when actual object is available. That's why this is known as
runtime or dynamic binding. On the hand,
private,
static and
final methods are resolved at compile time, because compiler knows that they can't be overridden and only possible methods are those, which are defined inside a class, whose reference variable is used to call this method. This is known as
static or compile time binding, all
private,
static and
final methods are resolved using static binding. This concept is also closely related to
method overloading and
method overriding. As dynamic binding happens when method overriding is possibility and overloaded method calls are resolved at compile time, because they are always defined in same class. In this article, we will learn few more
difference between Static and Dynamic Binding in Java, but before this let's see couple of examples of static and dynamic binding :