Static vs Dynamic Binding in Java

Course Curriculum

Static vs Dynamic Binding in Java

Static vs Dynamic Binding in Java

  • Static Binding: The binding which can be resolved at compile time by compiler is known as static or early binding. Binding of all the static, private and final methods is done at compile-time .

Why binding of static, final and private methods is always a static binding?
Static binding is better performance wise (no extra overhead is required). Compiler knows that all such methods cannot be overridden and will always be accessed by object of local class. Hence compiler doesn’t have any difficulty to determine object of class (local class for sure). That’s the reason binding for such methods is static.
Let’s see by an example

public class New_Class {
public static class baseclass {
static void print()
{
System.out.println("print in baseclass.");
}
}
public static class derivedclass extends baseclass {
static void print()
{
System.out.println("print in derivedclass.");
}
}

public static void main(String[] args)
{
baseclass A = new baseclass();
baseclass B = new derivedclass();
A.print();
B.print();
}
}
Before scrolling further down, Guess the output of the above program?

Output:

print in baseclass.
print in baseclass.

As you can see, in both cases print method of baseclass is called. Lets see how this happens

  • We have created one object of derivedclass and one object of baseclass with the reference of the baseclass.
  • Since the print method of baseclass is static, compiler knows that it will not be overridden in derivedclasses and hence compiler knows during compile time which print method to call and hence no ambiguity.

As an exercise, reader can change the reference of object B to derivedclass and then check the output.

Dynamic Binding: In Dynamic binding compiler doesn’t decide the method to be called. Overriding is a perfect example of dynamic binding. In overriding both parent and child classes have same method . Let’s see by an example

public class New_Class {
public static class baseclass {
void print()
{
System.out.println("print in baseclass.");
}
}

public static class derivedclass extends baseclass {
@Override
void print()
{
System.out.println("print in derivedclass.");
}
}

public static void main(String[] args)
{
baseclass A = new baseclass();
baseclass B = new derivedclass();
A.print();
B.print();
}
}
Output:

print in baseclass.
print in derivedclass.

Here the output differs. But why? Let’s break down the code and understand it thoroughly.

  • Methods are not static in this code.
  • During compilation, the compiler has no idea as to which print has to be called since compiler goes only by referencing variable not by type of object and therefore the binding would be delayed to runtime and therefore the corresponding version of print will be called based on type on object.

Important Points

  • private, final and static members (methods and variables) use static binding while for virtual methods (In Java methods are virtual by default) binding is done during run time based upon run time object.
  • Static binding uses Type information for binding while Dynamic binding uses Objects to resolve binding.
  • Overloaded methods are resolved (deciding which method to be called when there are multiple methods with same name) using static binding while overridden methods using dynamic binding, i.e, at run time.
Instance Initialization Block (IIB) in Java (Prev Lesson)
(Next Lesson) Why Java is not a purely Object-Oriented Language?
', { 'anonymize_ip': true });