| next>><<prevStatic and Dynamic Binding in JavaLast Updated : 3 Jan 2026This chapter explains the concepts of Static (compile-time) and Dynamic (runtime) binding in Java, showing how method calls are resolved either at compile time or runtime, with examples.What is Binding in Java?Bindingrefers to the process of linking a method call to its corresponding method body (the actual code that gets executed).When you write a method call in your code, the Java compiler or JVM must decide which method implementation to execute. This decision is calledbinding.There are two types of binding in Java:Static (Compile-time) Binding:The method to be called is determined atcompile time. It happens with private, final, and static methods and method overloading.Static bindingis also known asEarly binding.Dynamic (Runtime) Binding:The method to be called is determinedat runtimebased on theactual object, not the reference type. It happens with method overriding.Dynamic bindingis also known asLate binding.Before learning the static and dynamic binding, you must be familiar with Types. Let us understand the types first:Understanding TypeTo understand binding in Java, it is important to first understand the types of variables, references, and objects.1. variables have a typeEvery variable in Java has a type, which can be primitive or non-primitive.int data=30;Here data variable is a type of int.2. References have a typeA reference variable also has a type, which is the class it can refer to.class Dog{
public static void main(String args){
Dog d1;//Here d1 is a type of Dog
}
}3. Objects have a typeAn object is an instance of a particular Java class. However, it is also considered an instance of its superclass.class Animal{}
class Dog extends Animal{
public static void main(String args){
Dog d1=new Dog;
}
}Here d1 is an instance of Dog class, but it is also an instance of Animal.1. Static Binding in JavaWhen the type of an object is determined at compile time (by the compiler), it is known as static binding. Static binding occurs with private, final, or static methods, as well as with method overloading, because the compiler knows exactly which method to call.Example of Static BindingThe following example demonstrates the static binding:class Dog{
private void eat{System.out.println("dog is eating...");}
public static void main(String args){
Dog d1=new Dog;
d1.eat;
}
}2.Dynamic Binding in JavaWhen the type of the object is determined atrun-time, it is known asdynamic binding. Dynamic binding occurs with overridden methods, where the method call depends on the actual object being referred to, not the reference type.Example of Dynamic BindingThe following example demonstrates the dynamic binding:class Animal{
void eat{System.out.println("animal is eating...");}
}
class Dog extends Animal{
void eat{System.out.println("dog is eating...");}
public static void main(String args){
Animal a=new Dog;
a.eat;
}
}Output:dog is eating...In the above example object type cannot be determined by the compiler, because the instance of Dog is also an instance of Animal. So compiler doesn't know its type, only its base type.Difference Between Static and Dynamic BindingThe following table shows the difference between static and dynamic binding in Java:Static BindingDynamic BindingStatic binding occurs at compile time, when the compiler determines which method to call.Dynamic binding occurs at run time, when the method to be called is determined based on the actual object.It is used with private, final, static methods, and method overloading.It is used with overridden methods to achieve runtime polymorphism.In static binding, the method call depends on the reference type.In dynamic binding, the method call depends on the actual object type.Static binding provides compile-time polymorphism.Dynamic binding provides runtime polymorphism.Next TopicDowncasting and instanceof operator<<prevnext>> | Static Binding | Dynamic Binding | Static binding occurs at compile time, when the compiler determines which method to call. | Dynamic binding occurs at run time, when the method to be called is determined based on the actual object. | It is used with private, final, static methods, and method overloading. | It is used with overridden methods to achieve runtime polymorphism. | In static binding, the method call depends on the reference type. | In dynamic binding, the method call depends on the actual object type. | Static binding provides compile-time polymorphism. | Dynamic binding provides runtime polymorphism. |
| Static Binding | Dynamic Binding |
|---|---|
| Static binding occurs at compile time, when the compiler determines which method to call. | Dynamic binding occurs at run time, when the method to be called is determined based on the actual object. |
| It is used with private, final, static methods, and method overloading. | It is used with overridden methods to achieve runtime polymorphism. |
| In static binding, the method call depends on the reference type. | In dynamic binding, the method call depends on the actual object type. |
| Static binding provides compile-time polymorphism. | Dynamic binding provides runtime polymorphism. |
In this section, we will delve into the principles of Static (compile-time) and Dynamic (runtime) binding in Java, illustrating the process of resolving method calls at either compile time or runtime through practical examples.
What is Binding in Java?
Binding is the act of associating a method invocation with its respective method implementation, which comprises the executable code.
In the context of coding, when a method is invoked, the Java compiler or JVM needs to determine the specific method implementation to run. This determination process is known as binding.
In Java, there exist two kinds of binding:
- Static Binding: This binding type determines the method to be executed during compile time. It is applicable to private, final, and static methods, as well as method overloading. Static binding is also referred to as Early binding.
- Dynamic Binding: In this type of binding, the method to be executed is decided at runtime based on the actual object, rather than the reference type. It is associated with method overriding. Dynamic binding is also known as Late binding.
Prior to delving into the concepts of static and dynamic binding, it is essential to have a good grasp of Types. Let's begin by exploring the various types:
Understanding Type
In Java, before delving into binding, it is crucial to grasp the concepts surrounding variables, references, and objects. Each variable in Java is associated with a specific data type that determines the kind of data it can store.
In Java, each variable is associated with a type, which can fall into either the category of primitive or non-primitive.
int data=30;
The data variable is defined as an integer type.
- References are characterized by a specific type.
A reference variable is associated with a specific type, representing the class it is able to point to.
class Dog{
public static void main(String args[]){
Dog d1;//Here d1 is a type of Dog
}
}
- Objects have a type
An object in Java represents an instance of a specific class, while it is also recognized as an instance of the superclass.
class Animal{}
class Dog extends Animal{
public static void main(String args[]){
Dog d1=new Dog();
}
}
Here d1 is an instance of Dog class, but it is also an instance of Animal.
1. Static Binding in Java
Static binding refers to the determination of an object's type during compile time by the compiler. This binding is established with private, final, or static methods, and also with method overloading. In such cases, the compiler can accurately identify the specific method to invoke.
Example of Static Binding
An illustration showcasing static binding is presented below:
class Dog{
private void eat(){System.out.println("dog is eating...");}
public static void main(String args[]){
Dog d1=new Dog();
d1.eat();
}
}
2.Dynamic Binding in Java
Dynamic binding refers to the process of determining the type of an object during runtime. This mechanism is particularly evident when dealing with overridden methods, as the method invocation is based on the specific object being addressed, rather than the reference type.
Example of Dynamic Binding
An illustration showcasing dynamic binding is provided below:
class Animal{
void eat(){System.out.println("animal is eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("dog is eating...");}
public static void main(String args[]){
Animal a=new Dog();
a.eat();
}
}
Output:
dog is eating...
In the provided scenario, the compiler is unable to determine the object type since the instance of Dog is also considered an instance of Animal. Consequently, the compiler can only ascertain its base type and not its specific type.
Difference Between Static and Dynamic Binding
Below is a comparison table illustrating the distinction between static and dynamic binding in the Java programming language:
| Static Binding | Dynamic Binding |
|---|---|
| Static binding occurs at compile time, when the compiler determines which method to call. | Dynamic binding occurs at run time, when the method to be called is determined based on the actual object. |
| It is used with private, final, static methods, and method overloading. | It is used with overridden methods to achieve runtime polymorphism. |
| In static binding, the method call depends on the reference type. | In dynamic binding, the method call depends on the actual object type. |
| Static binding provides compile-time polymorphism. | Dynamic binding provides runtime polymorphism. |