Site icon DataFlair

Static Binding Vs Dynamic Binding in Java

Static binding vs Dynamic Binding

Static Binding and Dynamic Binding

Free Java courses with 37 real-time projects - Learn Java

In this Java tutorial, we will learn Java static binding vs dynamic binding in detail. Also, we will discuss the feature wise difference between static binding and dynamic binding in Java Programming Language. Moreover, we will go through the static binding definition and definition of a dynamic binding in Java. Along with this, we will see an example of static binding vs dynamic binding in Java.

So, let’s start a comparison of Static Binding vs Dynamic Binding in Java.

Static Binding Vs Dynamic Binding in Java

In this static binding, the binding can resolve at runtime or compile time.
The binding process of all static, final and private methods is done at compile time.

Overriding in Java can be considered as the best example of dynamic binding as both parent and child class have the same method, that is it doesn’t decide the method to be called.

What is Static Binding in Java?

Binding of static, final and private methods is always a static binding because a static binding gives better performance and they cannot be overridden and hence will always be accessed by the object of some local class.

Example –

public class NewClass
{
   public static class superclass
   {
       static void print()
       {
           System.out.println("print in superclass.");
       }
   }
   public static class subclass extends superclass
   {
       static void print()
       {
           System.out.println("print in subclass.");
       }
   }
   public static void main(String[] args)
   {
       superclass A = new superclass();
       superclass B = new subclass();
       A.print();
       B.print();
   }
}

Output –
Print in superclass.
Print in superclass.
In the example above both cases give the same result because –

What is Dynamic Binding in Java?

Example of dynamic binding –

public class NewClass
{
   public static class superclass
   {
       void print()
       {
           System.out.println("print in superclass.");
       }
   }
   public static class subclass extends superclass
   {
       @Override
       void print()
       {
           System.out.println("print in subclass.");
       }
   }
   public static void main(String[] args)
   {
       superclass A = new superclass();
       superclass B = new subclass();
       A.print();
       B.print();
   }
}

Output –
Print in superclass.
Print in subclass.
The output differs because –

Essential Points for Static Binding vs Dynamic Binding

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Here, we will discuss some important points related to Static vs Dynamic Binding in Java:

So, this was all about Static Binding vs Dynamic Binding in Java. Hope you like our explanation.

Conclusion

In this tutorial we learned about the static vs dynamic binding, their difference and important points to remember. Moreover, we discussed static binding and dynamic binding with the example and given output. At last, we saw some essential points for dynamic and static binding. Furthermore, if you have any queries regarding the difference between Static and Dynamic Binding in Java, feel free to ask in the comment section.

Exit mobile version