Saturday, March 10, 2012

What is Static and Dynamic binding in Java with Example

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) Static binding uses Type(Class in Java)  information for binding while Dynamic binding uses Object to resolve binding.

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.

public class StaticBindingTest {
 
    public static void main(String args[])  {
       Collection c = new HashSet();
       StaticBindingTest et = new StaticBindingTest();
       et.sort(c);
     
    }
   
    //overloaded method takes Collection argument
    public Collection sort(Collection c){
        System.out.println("Inside Collection sort method");
        return c;
    }
 
   
   //another overloaded method which takes HashSet argument which is sub class
    public Collection sort(HashSet hs){
        System.out.println("Inside HashSet sort method");
        return hs;
    }
     
}

Output:
Inside Collection sort method

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 binding in java which occurs during run time and instead of Type or Class information, Object is used to resolve method calls.

public class DynamicBindingTest {

    public static void 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
    }
}

class Vehicle {

    public void start() {
        System.out.println("Inside start method of Vehicle");
    }
}

class Car extends Vehicle {

    @Override
    public void start() {
        System.out.println("Inside start method of Car");
    }
}

Output:
Inside start method of Car

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.

15 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.

Swati said...

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.

Deepa said...

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

Java said...

Its as simple as this:
static binding - compile time resolution (mainly overloaded method)
dynamic binding - run time resolution (mainly overridden method)

Prateek said...

@Swati
Yes 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.

Anonymous said...

i think in static binding example output is
Inside HashSet sort method
as the following example
class A
{
public void hello()
{
System.out.println("hello in A");
}
}
class B extends A
{
public void hello()
{
System.out.println("hello in B");
}
}
class C
{
public static void main(String... args)
{
A a = new B();
a.hello();
}
}
output is----
hello in B

Anonymous said...

good one

Anonymous said...

No Dear, You are taking an example of overridding(same parameter in method having same signature)

Anonymous said...

This is a very good tutorial. But if you accept advice, you repeat sentences a lot. Usually experienced programmers need to get in depth as fast as possible :)

Rafael B.C. said...

Hello, this is a great post as other in this blog. However i see this line:

"Overloaded methods are bonded using static binding while overridden methods are bonded during compile time"

It shouldn't be "... overriden methods are bonded during runtime through dynamic binding" ?

Regards,

Javin @ Java Classloder Working said...

@Rafael B.C., good pickup. Surprised to see that typo for so long. Yes, Overridden methods are bonded during runtime, as objects are only created at runtime. Thanks for pointing that out.

Anonymous said...

Its a very good explanation, thanks. Millions of sites in the WEB use words "static context" but nobody seems to understand what it means

Duke Banerjee said...

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.

Anonymous said...

thanqu for give the simle example of static binding.

Syed Ali said...

What is the difference between Static polymorphism and Dynamic polymorphism???

Post a Comment