Static and Dynamic Binding in Java with Example

What is Binding in Java?


The connecting (linking) between a method call and method body/definition is called binding in Java. Look at the below figure in which we have defined a class X. It has two methods m1() and m2().

The x.m1(); (method call) is linked with m1() method whereas, x.m2(); is linked with m2() method. This linking is called binding.

Binding in Java

Types of Binding in Java


There are two types of binding in Java. They are as follows:

  • Static Binding (also known as Early Binding)
  • Dynamic Binding (also known as Late Binding)

Let’s understand static binding and dynamic binding one by one with example programs.

Static Binding in Java


The binding that happens during compilation is called static binding in Java. This binding is resolved at the compiled time by the compiler.

In static binding, the java compiler does not check the type of object to which a particular reference variable is pointing to it.


Java compiler just checks which method is going to be called through reference variable and method definition exists or not.

This binding is also known as early binding because it takes place before the program actually runs. An example of static binding is method overloading.

Let’s take an example program where we will calculate the addition of two and three numbers using the method overloading technique.

Program code 1:

package methodOverloading; 
public class Addition { 
void add(int x, int y) 
{ 
    int sum = x + y; 
    System.out.println("Sum of two numbers: " +sum); 
} 
void add(int x, int y, int z) 
{ 
    int sum = x + y + z; 
    System.out.println("Sum of three numbers: " +sum); 
} 
public static void main(String[] args) 
{ 
    Addition a = new Addition(); 
    a.add(10, 20); // Calling add() method with passing two argument values. 
    a.add(20, 30, 40); // Calling add() method with passing three argument values. 
 } 
}
Output: 
       Sum of two numbers: 30 
       Sum of three numbers: 90

In the above preceding code, Java compiler did not check the type of object. It just checked only method call through reference variable and method definition.


If there is any private, final, or static method in a class, all these method binding are called static binding. For example:

Program code 2:

public class Lion { 
private void eat() 
{ 
   System.out.println("Lion eats flesh"); 
} 
static void live() 
{ 
   System.out.println("Lion lives in Jungle"); 
} 
public static void main(String[] args) 
{ 
   Lion l = new Lion(); 
   l.eat(); 
   Lion.live(); 
 } 
}
Output: 
       Lion eats flesh 
       Lion lives in Jungle

Why binding of private, static, and final methods are always static binding?


Static binding is better performance-wise because java compiler knows that all such methods cannot be overridden and will always be accessed by object reference variable.

Hence, the compiler doesn’t have any difficulty in binding between a method call and method definition. That’s why binding for such methods is always static.

Dynamic Binding in Java


The binding which occurs during runtime is called dynamic binding in Java. This binding is resolved based on the type of object at runtime. In dynamic binding, the actual object is used for binding at runtime.

Dynamic binding is also called late binding or runtime binding because binding occurs during runtime. An example of dynamic binding is method overriding.

Let’s take an example program based on dynamic binding in Java.

Program code 3:

package dynamicBinding; 
public class Animal { 
void eat() 
{ 
   System.out.println("Animals eat both plants and flesh"); 
 } 
} 
public class Lion extends Animal { 
void eat() 
{ 
    System.out.println("Lions eat flesh because they are carnivore"); 
 } 
} 
public class DynamicBindingTest { 
public static void main(String[] args) 
{ 
    Animal a = new Animal();
 // This statement will call eat() method of Animal class because the reference variable is pointing to object of animal class. 
    a.eat(); 
    
    Animal a1 = new Lion(); 
 // This statement will call eat() method of Lion class because the reference variable is pointing towards the object of Loin class. 
    a1.eat(); 
 } 
}
Output: 
       Animals eat both plants and flesh 
       Lions eat flesh because they are carnivore

In the preceding example, object type cannot be determined by the compiler. Therefore, JVM resolved the method calls based on the type of object at runtime.


If you’re struggling with your assignment, don’t worry—you’re not alone. Even the most proficient coders have to tackle tough tasks from time to time, so it’s totally normal for things to get overwhelming.

Fortunately, there are plenty of resources out there that can provide JavaScript coding help, so you can get the job done and move on to the next task.

Whether you’re looking for one-on-one guidance with your assignment or just a quick answer to a tricky coding problem, there are plenty of options available online that can help make completing your JavaScript homework a little less stressful.

Difference between Static and Dynamic Binding in Java


There are the following differences between static and dynamic binding. They are as follows:

1. Static binding occurs at compile-time while dynamic binding occurs at runtime.

2. In static binding, actual object is not used whereas, actual object is used in the dynamic binding.

3. Static binding is resolved at compile time whereas, dynamic binding is resolved at runtime.

4. Static binding is also called early binding because it happens during compilation whereas, dynamic binding is called late binding because it happens during runtime.

5. An example of static binding is method overloading whereas, the example of dynamic binding is method overriding.

6. Private, static, and final methods show static binding because they cannot be overridden whereas, except private, static, and final methods, other methods show dynamic binding because they can be overridden.


In this tutorial, we have covered almost all the important points related to static and dynamic binding in Java with the help of example programs. Hope you will have understood the basic concepts of static and dynamic binding and practiced all the example programs based on it. In the next, we will understand the most important topic exception handling in Java.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love