
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Static vs Dynamic Binding in Java
In Java static binding refers to the execution of a program where type of object is determined/known at compile time i.e when compiler executes the code it know the type of object or class to which object belongs.While in case of dynamic binding the type of object is determined at runtime.
Also static binding uses type of class to bind while dynamic binding uses type of object as the resolution happens only at runtime because object only created during runtime due to which dynamic binding becomes slower than in case of static binding.
As private,final and static modifiers binds to the class level so methods and variables uses static binding and bonded by compiler while the other methods are bonded during runtime based upon runtime object.
In general we can say that overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding.
Example
import java.util.Collection; import java.util.Collections; import java.util.HashSet; public class StaticDynamicBinding { public static void main(String[] args) { Collection<String> col = new HashSet<>(); col.add("hello"); StaticDynamicBinding sdObj = new StaticDynamicBinding(); sdObj.print(col); StaticDynamicBinding sdObj1 = new StaticDynamicBindingClass(); sdObj1.print(col); } void print(Collection<String> col) { System.out.println("in collection method"); } void print(HashSet<String> hs) { System.out.println("in hashset method"); } } class StaticDynamicBindingClass extends StaticDynamicBinding { void print(Collection<String> col) { System.out.println("in collection method of child class"); } }
Output
in collection method in collection method of child class
- Related Questions & Answers
- Static binding vs Dynamic binding in C#
- Static binding and dynamic binding in Java
- Difference between Static binding and dynamic binding in Java
- What are differences between static binding and dynamic binding in Java?
- Difference Between Static and Dynamic Binding
- Explain the difference between static and dynamic binding in Java.
- Static Testing Vs Dynamic Testing
- What is dynamic binding in Java?
- Dynamic Binding in C#
- What is static binding in Java?
- What is dynamic binding in C#?
- What is static binding in C#?
- static keyword in C++ vs Java
- Static methods vs Instance methods in Java
- Static vs. Non-Static method in C#