Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Static Binding vs. Dynamic Binding in Java

DZone's Guide to

Static Binding vs. Dynamic Binding in Java

Struggling to understand the difference between static and dynamic binding?

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

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

Dynamic Binding in Java

Overriding in Java can be considered the best example of dynamic binding as both parent and child classes have the same method — that is, it doesn’t decide on 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.

Here is an example of static binding:

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:

  • The reference here for subclass and superclass is the same, that is a single object.
  • Since it cannot be overridden in a subclass, i.e. the superclass and compiler, it knows and there is no ambiguity.

What Is Dynamic Binding in Java?

Here is an 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:

  • Methods here are not static.
  • Amid binding, the compiler has no clue as to which print must be called since compiler goes just by referencing variable not by kind of object and along these lines the binding would be deferred to runtime and in this way the comparing adaptation of print will be called in view of a sort on the question.

Essential Points for Static Binding Vs. Dynamic Binding

Here, we will discuss some important points related to static and dynamic binding in Java:

  • Private, last and static individuals utilize static authoritative while for virtual techniques restricting finish amid runtime in view of runtime object.
  • Static binding in Java uses type data for official while Dynamic binding in Java restricting uses objects to determine to bind.
  • Overloaded methods are settled utilizing static binding while overridden methods utilizing dynamic binding, i.e, at runtime.

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

Conclusion

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

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,static binding ,dynamic binding ,binding ,binding in java ,overriding ,performance

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}