Static Binding and Dynamic Binding in Java

There are two types of binding

  • static binding (also known as early binding)
  • dynamic binding (also known as late binding)

Static binding

When type of the object is determined at compiled time(by the compiler), it is known as static binding. If there is any private, final or static method in a class, there is static binding.

Example of static binding

			 
 class Dog {
	private void eat() {
		System.out.println("dog is eating...");
	}

	public static void main(String args[]) {
		Dog d = new Dog();
		d.eat();
	}
}

Dynamic binding

When type of the object is determined at run-time, it is known as dynamic binding.

Example of 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.