Java what is static binding

Static and dynamic binding in java

Association of method call to the method body is known as binding. There are two types of binding: Static Binding that happens at compile time and Dynamic Binding that happens at runtime. Before I explain static and dynamic binding in java, lets see few terms that will help you understand this concept better.

What is reference and object?

class Human < . >class Boy extends Human < public static void main( String args[]) < /*This statement simply creates an object of class *Boy and assigns a reference of Boy to it*/ Boy obj1 = new Boy(); /* Since Boy extends Human class. The object creation * can be done in this way. Parent class reference * can have child class reference assigned to it */ Human obj2 = new Boy(); >>

Static and Dynamic Binding in Java

As mentioned above, association of method definition to the method call is known as binding. There are two types of binding: Static binding and dynamic binding. Lets discuss them.

Static Binding or Early Binding

The binding which can be resolved at compile time by compiler is known as static or early binding. The binding of static, private and final methods is compile-time. Why? The reason is that the these method cannot be overridden and the type of the class is determined at the compile time. Lets see an example to understand this:

Читайте также:  Docker nginx php fpm xdebug

Static binding example

Here we have two classes Human and Boy. Both the classes have same method walk() but the method is static, which means it cannot be overriden so even though I have used the object of Boy class while creating object obj, the parent class method is called by it. Because the reference is of Human type (parent class). So whenever a binding of static, private and final methods happen, type of the class is determined by the compiler at compile time and the binding happens then and there.

class Human < public static void walk() < System.out.println("Human walks"); >> class Boy extends Human < public static void walk()< System.out.println("Boy walks"); >public static void main( String args[]) < /* Reference is of Human type and object is * Boy type */ Human obj = new Boy(); /* Reference is of HUman type and object is * of Human type. */ Human obj2 = new Human(); obj.walk(); obj2.walk(); >>

Dynamic Binding or Late Binding

When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding. Method Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have same method and in this case the type of the object determines which method is to be executed. The type of object is determined at the run time so this is known as dynamic binding.

Dynamic binding example

This is the same example that we have seen above. The only difference here is that in this example, overriding is actually happening since these methods are not static, private and final. In case of overriding the call to the overriden method is determined at runtime by the type of object thus late binding happens. Lets see an example to understand this:

class Human < //Overridden Method public void walk() < System.out.println("Human walks"); >> class Demo extends Human < //Overriding Method public void walk()< System.out.println("Boy walks"); >public static void main( String args[]) < /* Reference is of Human type and object is * Boy type */ Human obj = new Demo(); /* Reference is of HUman type and object is * of Human type. */ Human obj2 = new Human(); obj.walk(); obj2.walk(); >>

As you can see that the output is different than what we saw in the static binding example, because in this case while creation of object obj the type of the object is determined as a Boy type so method of Boy class is called. Remember the type of the object is determined at the runtime.

Читайте также:  Python создать класс type

Static Binding vs Dynamic Binding

Lets discuss the difference between static and dynamic binding in Java.

  1. Static binding happens at compile-time while dynamic binding happens at runtime.
  2. Binding of private, static and final methods always happen at compile time since these methods cannot be overridden. When the method overriding is actually happening and the reference of parent type is assigned to the object of child class type then such binding is resolved during runtime.
  3. The binding of overloaded methods is static and the binding of overridden methods is dynamic.

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Comments

I was searching for static vs dynamic binding and found this, very helpful for me. Thank you so much for explaining these concepts with examples

I am glad that you liked it. let me know if you have any question regarding binding in java, I would be happy to help you out.

I understood the above points about early and late binding, however can you please explain binding in terms of references? It would be really helpful for me.

Harry – let me explain your doubt with the help of few examples –
Static binding:
it means that the references are resolved during compile time by compiler
lets say – Car obj = new Car();
obj.drive(); Compiler won’t face any difficulty while resolving the conflict during compilation. Dynamic Binding:
Another example – public void myMethod(Object obj) ((Car)obj).drive();
>
In this case compiler can’t resolve the object reference during compilation. Hence this can be resolved during run time only. This is called dynamic binding.

Iamnot clear with the explaination actually do you mean dynamic polymorphism and dynamic binding are same

Hi Chaitanya, as you say in dynamic binding that, compiler gets confused while choosing method either from parent class or from child class. But how does it make right choice?

Hi Akshay, References can only be resolved at runtime. Lets take the above example to understand this:

The fact that myobj is refers to the object of its child class Boy is resolved at runtime only. Hence, at compile time the compiler can’t be sure if the call to the method ‘walk()’ on these references actually refer to which version of the method – the super class version or the sub class version. That’s the reason such type of binding happens at runtime and known as dynamic binding.

If the references variables are resolved at runtime then what will you call this below: A ob = new ob():
ob.Show(); so here there is nothing ambiguous, so what should we call it early or late binding?

hi Chaitanya , your tutorial was very nice. Thank you so much for woderful explanation with examples.

Источник

Static and Dynamic Binding in Java with Example

Scientech Easy

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:

1. Static Binding (also known as Early Binding).
2. 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.

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:

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:

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(); a.eat(); // It will call eat() method of Animal class because the reference variable is pointing to object of animal class. Animal a1 = new Lion(); a1.eat(); // It will call eat() method of Lion class because the reference variable is pointing towards the object of Loin class. >>
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.

Hope that this tutorial has covered almost all the important points related to static and dynamic binding in java with many example programs. I hope you will have understood this easy topic and enjoyed it.
Thanks for reading.
Next ⇒ Exception Handling in Java ⇐ PrevNext ⇒

Источник

Оцените статью