Static and Dynamic Binding in Java

The connection between method call to the method body is known as binding. There are two types of binding in java, Static binding that happens at compile time and Dynamic binding that happens at runtime.


Static Binding

The binding which can be resolved at compile time by the compiler is known as static or early binding.

The binding of static, private and final methods is compile time binding because these method cannot be overridden and the type of the class is determined at the compile time. These methods can only be accessed by the object of the local class. Therefore the binding of these methods always takes place during compilation.

Generally, we can say that overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding.


Differences between static and dynamic binding

Static Binding Dynamic Binding
It happens at compile time for which is referred to as early binding. It happens at runtime so it is also referred as late binding.
Method overloading is static binding. Method overriding is static binding.
Actual object is not used in static binding. Real objects use dynamic binding.

Example program of static binding in java

class Shoe {
  public static void printDetails() {
    System.out.println("I am a Shoe");
  }
}

class Nike extends Shoe {
  public static void printDetails() {
    System.out.println("I am a Nike Shoe");
  }
}

public class StaticBinding {
  public static void main(String[] args) {
    // Creating object of super and sub class
    Shoe sh = new Shoe();
    Shoe nk = new Nike();

    // Calling printDetails methods
    sh.printDetails();
    nk.printDetails();
  }
}
Output
I am a Shoe
I am a Shoe

In the above program, we got the same output from the parent object as well as child object's invocation to printDetails() method due to following reason:

  • We have created one object of subclass and one object of the superclass with the reference of the superclass.

  • Since the method is static, the compiler is aware that this method can not be overridden in the child class and it knows which method to call. Therefore there is no ambiguity and the output is the same for both cases.


Dynamic Binding

When compiler is not able to resolve the method call at compile time, such binding is known as Dynamic binding. In such scenarios, th e compiler resolves the method call binding during the execution of the program.

Method Overriding in java is an example of Dynamic binding where both the parent class and the child class have the same method and the type of the object determines which method is going to be invoked.

class Shoe {
  public void printDetails() {
    System.out.println("I am a Shoe");
  }
}

class Nike extends Shoe {
  public void printDetails() {
    System.out.println("I am a Nike Shoe");
  }
}

public class DynamicBinding {
  public static void main(String[] args) {
    // Creating object of super and sub class
    Shoe sh1 = new Shoe();
    Shoe sh2 = new Nike();

    // Calling printDetails methods
    sh1.printDetails();
    sh2.printDetails();
  }
}
Output
I am a Shoe
I am a Nike Shoe

In this program, we can see that the output is different than the static binding program above due to following reason:

  • We have not declared the methods as static in the code.

  • Object type cannot be determined by the compiler, because the instance of Nike is also an instance of Shoe. Hence, compiler doesn't know its type, only its base type till it gets executed.