Home >>Java Tutorial >Static Binding and Dynamic Binding in Java

Static Binding and Dynamic Binding in Java

Static Binding and Dynamic Binding in Java

Static Binding and Dynamic Binding in Java are basically the types of the binding that exits in the language and the simplest explanation of the binding is that it is the process of connecting a method call to the method body. There are two types of the binding in the Java language that are depicted below:

  • Static binding
  • Dynamic binding

Let's get an idea about both the binding and then we will explain it one by one.

Static binding in Java basically refers to the execution of a program where the type of the object is determined/known at the compile time in simple words when the compiler executes the code it has already known the type of the object or class to which the object belongs. On the other hand, in the case of dynamic binding in Java language the type of object is generally determined at the runtime.

Static binding generally uses type of the class in order to bind while the dynamic binding uses type of object; this is because the resolution occurs only at the runtime as the object is only created during the runtime that leads to the dynamic binding becoming slower than in case of static binding in Java.

In the Java language, private, final and the static modifiers are used to bind to the class level hence the methods and variables generally uses the static binding and bonded by the compiler while the other methods are basically bonded during the runtime that is based upon the runtime object. Usually, the programmers can say that overloaded methods are basically bonded by the use of the static binding while the overridden methods are generally bonded using the dynamic binding.

1. Static Binding

The binding in Java language that can be resolved at the compile time by the compiler itself is known as the static or early binding. Generally, binding of all the private, static, and the final methods are done at the compile-time. In general, the static binding delivers a better performance as there is no extra overhead required.

The working of the static binding has a theory and that is: the compiler knows that all these methods generally cannot be overridden and they will always be accessed by the object of local class. Hence, the compiler doesn’t find it difficult to determine the object of the class. And this result to the binding of such methods is generally static.

Here is an example of the static binding that will help you understand the physical aspect of it and will depict the working of the static binding in the Java Language:

public class BaseClass 
{ 
	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 sup = new superclass(); 
		superclass sub = new subclass(); 
		sup.print(); 
		sub.print(); 
	} 
}
Output:
print in superclass.
print in subclass.

2. Dynamic Binding in Java

In the dynamic binding in Java language the compiler isn’t the one that decide the method to be called. The finest example of the dynamic binding is the overriding. What generally happens in the overriding is that both the parent and child classes have the exact same method. In very simple words, it can be understood as; whenever the type of the object is generally determined at the run-time then it is called as the dynamic binding in the Java.

Here is an example of the dynamic binding in Java that will help you in getting to the core of the concept along with depicting the physical aspect of it:

public class BaseClass 
{ 
	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 sup = new superclass(); 
		superclass sub = new subclass(); 
		sup.print(); 
		sub.print(); 
	} 
}
Output:
print in superclass.
print in subclass.

Points to Remember

Here are the main points that should be remembered while working with the static and dynamic binding in Java:

  • Private, final and the static members that are basically the methods and variables generally use the static binding on the other hand, for the virtual methods dynamic binding is done during the run time generally based upon the run time object.
  • Static binding in Java generally uses Type information in order to bind on the other hand; dynamic binding in Java basically uses the objects to resolve the binding.
  • Overloaded methods are resolved with the help of static binding, in simple terms, it can be explained as deciding that which method to be called when there are multiple methods with the exact same name, on the other hand, overridden methods generally uses the dynamic binding at the run time.

No Sidebar ads