Friday, March 10, 2017

Static and dynamic binding in Java are two important concept which Java programmer should be aware of. this is directly related to execution of code. If you have more than one method of same name (method overriding) or two variable of same name in same class hierarchy it gets tricky to find out which one is used during runtime as a result of there reference in code. This problem is resolved using static and dynamic binding in Java. For those who are not familiar with binding operation, its process used to link which method or variable to be called as result of there reference in code. Most of the references is resolved during compile time but some references which depends upon Object and polymorphism in Java is resolved during runtime when actual object is available. In this Java tutorial we will see some examples of static and dynamic binding and differences between static binding and dynamic binding in Java.

Difference between Static and Dynamic binding in Java:

Difference between static and dynamic binding is a popular Java programming interview question which tries to explore candidates knowledge on have compiler and JVM finds which methods to call if there are more than one method of same name as it's case in method overloading and overriding. This is also best way to understand what is static binding and what is dynamic binding in Java. In next section we will difference between both of them.

Static Binding vs Dynamic binding Java

Here are few important difference between static and dynamic binding in Java written in point format. knowledge of static and dynamic binding is require to understand Java code and find out any bugs and issue while running Java program. It also helps in troubleshooting and debugging in Java.

1) Static binding in Java occurs during Compile time while Dynamic binding occurs during Runtime.

2) private, final and static methods and variables uses static binding and bonded by compiler while virtual methods are bonded during runtime based upon runtime object.

3) Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime. Here is an example which will help you to understand both static and dynamic binding in Java.

Static Binding Example in Java

Here is an example of static binding in java, which will clear things on how overloaded methods in java are bonded during compile time using Type information.

In above example of static binding in Java we have overloaded sort() method, one of which accept Collection and other accept HashSet. we have called sort() method with HashSet as object but referenced with type Collection and when we run method with collection as argument type gets called because it was bonded on compile time based on type of variable (Static binding) which was collection.

Example of Dynamic Binding in Java

In last section we have seen example of static binding which clears things that static binding occurs on compile time and Type information is used to resolve methods. In this section we will see example of dynamic bindingin java which occurs during run time and instead of Type or Class information, Object is used to resolve method calls.

publicclass DynamicBindingTest {

publicstaticvoid main(String args[]){
Vehicle vehicle = new Car();//here Type is vehicle but object will be Car
vehicle.start();//Car's start called because start() is overridden method}}

In this example of Dynamic Binding we have used concept of method overriding. Car extends Vehicle and overrides its start() method and when we call start() method from a reference variable of type Vehicle, it doesn't call start() method from Vehicle class instead it calls start() method from Car subclass because object referenced by Vehicle type is a Car object. This resolution happens only at runtime because object only created during runtime and called dynamic binding in Java. Dynamic binding is slower than static binding because it occurs in runtime and spends some time to find out actual method to be called.

That's all on difference between static and dynamic binding in java. bottom line is static binding is a compile time operation while dynamic binding is a runtime. one uses Type and other uses Object to bind. static, private and final methods and variables are resolved using static binding which makes there execution fast because no time is wasted to find correct method during runtime.

33 comments
:

Joshua
said...

I think main difference is as per name suggest static binding is static and dynamic binding is dynamic means more flexible. also isn't it dynamic binding also called late binding ? which implies it happen late during execution time.

Does both static and dynamic bonding related to polymorphism ? I guess no. because all final static and private methods and variable resolved during compile time no matter whether they are overloaded or overridden.

I was looking for difference between static vs dynamic binding when I found this tutorial. Now I know exactly what is static binding and what is dynamic binding in Java. very precise and simple example helps me to understand both static and dynamic binding in java. Thanks

@SwatiYes both overloading and overriding is form of polymorphism because they are basically same name but different functionality. But I agree private and static method are resolved during compile time and also referred as Static binding.

This is an example of dynamic dispatch or polymorphism, not dynamic binding. All names are statically bound at compile time. If you chose a different name in Car for the start method (and kept the @Override annotation), the example wouldn't compile because the name is statically bound. A perfect example of Java being a statically bound language (and not having dynamic binding) is the necessity of the Visitor pattern.

A better example of dynamic binding in Java is JavaBeans, because the name of the property has to be looked up at runtime and bound to the correct get/set method.

Polymorphism only work on overriding, because polymorphism works by checking on "object type" of the object (is-A term), unlike overloading because it works by checking on "reference type" of the object

sorry guys, but i want to know that can we say this , overloading is compile time polymorhisim and overriding is run-time polymorphisim, but in the case of static methods in overriding we can call it again compile time polymorphisim. tht means in overriding thre can be compile time or runtime polymorphisim but in the case of overloading it can only be compile time polymorphisim..? just clear my concept if any one knows.

I know this is the Inheritance concept, But can you please explain how it works? Bcoz here reference type is super class assigned by object of subclass. (In the dynamic binding example, mentioned that "......because object referenced by Vehicle type is a Car object") so in the below example also object referenced by 'A' type is a 'B' object, then it should executes method in B class but it didn't. Please explain.

What type of binding is there in below case :class A{}class B extends A { public void method(){} }

What type of binding is there in below case :class A{}class B extends A { public void method(){} }

A obj = new B();obj.method(); // type of binding ??

Ans: It is Run-time binding no doubt, but above code will give Compile time error, cannot find the symbol

because you are calling the non-overridden method of B class with the reference of class A. So, do remember one that you can only call the overridden methods using class A (Base class) reference and won't able to call non-overridden methods of class B(Derived class).

@Anonymous, you are right. The compile time error is because there is no such method in class A. if you want to call a method only defined in class B, you need to cast the object and store in a reference of class B.

Hi Everyone, I don't think, there is any existence for Static binding for Method Overloading, becuase compiler always using the "InvokeVirtual" Instruction for binding the Overloaded methods. below is the example of Method Overlaoding:import java.lang.*;public class A{

public void m1(){ System.out.println("Hi this is without parameterized");}

public void m1(int i){ System.out.println("hi this is from Parameterised"+i);}

public void m1(String s1){

System.out.println("this is from String"+s1);}

public static void main(String args[]){ A a = new A(); a.m1(); a.m2(10); a.m3("ABC");}

}

=======================================After compilation you can see the compiler's instructions which clearly shows it always bind these methods as Runtime.

import java.lang.*;public class A{

public void m1(){ System.out.println("Hi this is without parameterized");}

public void m1(int i){ System.out.println("hi this is from Parameterised"+i);}

public void m1(String s1){

System.out.println("this is from String"+s1);}

public static void main(String args[]){ A a = new A(); a.m1(); a.m2(10); a.m3("ABC");}

what is compile time? and what actually happens when i type javac during the compilation process, if all the class loading and other dynamic linking and loading happens in the run time. Then what actaully happens during the compile time?