JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Difference between early (static) binding vs late (dynamic) binding in Java

In order to understand the difference between static and dynamic binding in Java, it's important to first learn what is binding? Binding means the link between reference and actual code e.g. when you refer a variable it's bonded to the code where it is defined, similarly when you call a method, it's linked to the code where a method is defined. There are two types of method binding in Java, static binding and dynamic binding. When a method is called in Java it's bonded to the actual code either at compile time or runtime, when the program is actually started and objects are created. As the name suggest, static binding is more of static nature hence it occurs at compile time i.e. your code knows which method to call once you compiled your Java source file into a class file. Since it happens early in program's life cycle it is also known as early binding in Java.

On the other hand, dynamic binding occurs at runtime, when JVM starts your program. This time which method to call is figured out by an actual object, which information was not available at compile time because objects are created at runtime. Since it happens late in the program life cycle, it is also known as late binding in Java.


So, this was the fundamental difference between static binding vs dynamic binding in Java, one occurs early at compile time using the type of reference variable, while other occur late at runtime by using actual objects. Let's see a couple of more difference to understand this concept better. It is also one of the frequently asked Java questions, especially during first few rounds, so spending time here will be definitely worth it.


Early vs Late  binding Java

There are many points on which you can differentiate static binding and dynamic binding, but the most important are how it is used by JVM. Have you ever thought how JVM decides which methods to call if there are more than one method in the scope with the same name? If you have ever used method overloading or method overriding then you know that it's possible in Java to have multiple methods with the same name. JVM uses both static and dynamic binding to find the right method in Java.


Static and binding example in Java

In this program, you will see that virtual methods are not bonded at compile time using static binding because if that happens then method from super class would have been called as is the case of the static method which is bonded early. Since, method from subclass is called its proved that the actual object is used for function binding at runtime, hence dynamic binding is used to bind virtual methods.



public class Main {

  public static void main(String[] args) {

    // An example of static and dynamic binding in Java
    Insurance current = new CarInsurance();
    
    // dynamic binding based upon object
    int premium = current.premium(); 
    
    // static binding based upon class
    String category = current.category();
    
    System.out.println("premium : " + premium);
    System.out.println("category : " + category);
    
  }
  
  

}

class Insurance{
  public static final int LOW = 100;
  
  public int premium(){
    return LOW;
  }
  
  public static String category(){
    return "Insurance";
  }
  
}

class CarInsurance extends Insurance{
  public static final int HIGH = 200;
  
  public int premium(){
    return HIGH;
  }
  
  public static String category(){
    return "Car Insurance";
  }
  
}

Output
premium : 200
category : Insurance


You can see here that the call to premium() method executed the method from subclass while call to category() method is executed from the superclass. This happens because premium() is virual method and resolved using late binding while category() is a static method and resolved using static binding at compile time using classname. Your can further check out Java : How to program by Dietel to learn more about the difference between the static and non-static method in Java.







Difference between early and late binding in Java


1) The static binding occurs at compile time while dynamic binding happens at runtime.



2) Since static binding happens at an early stage of program's life cycle, it also is known as early binding. Similarly, dynamic binding is also known as late binding because it happens late when a program is actually running.



3) Static binding is used to resolve overloaded methods in Java, while dynamic binding is used to resolve overridden methods in Java.



4) Similarly, private, static and final methods are resolved by static bonding because they can't be overridden and all virtual methods are resolved using dynamic binding.



5) The actual object is not used in case of Static binding, instead, the type information i.e. the type of reference variable is used to locate the method. On the other hand, dynamic binding uses an actual object to find the right method in Java.



Here is a nice exercise which is based on the concept of static and dynamic binding in Java, let's see if you can answer this question:

Difference between early and late binding in Java



.