DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Static Binding vs. Dynamic Binding in Java

Static Binding vs. Dynamic Binding in Java

Struggling to understand the difference between static and dynamic binding?

Rinu Gour user avatar by
Rinu Gour
·
Jan. 11, 19 · Presentation
Like (11)
Save
Tweet
Share
25.44K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Binding (linguistics) Java (programming language)

Published at DZone with permission of Rinu Gour. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Policy-as-Code? An Introduction to Open Policy Agent
  • Easy Smart Contract Debugging With Truffle’s Console.log
  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • The Future of Cloud Engineering Evolves

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: