Binding is a mechanism creating link between method call and method actual implementation. As per the polymorphism concept in Java, object can have many different forms. Object forms can be resolved at compile time and run time.
Java Static Binding
Static binding refers to the process in which linking between method call and method implementation is resolved at compile time. Static binding is also known as compile-time binding or early binding.
Characteristics of Java Static Binding
- Linking − Linking between method call and method implementation is resolved at compile time.
- Resolve mechanism − Static binding uses type of the class and fields to resolve binding.
- Example − Method overloading is the example of Static binding.
- Type of Methods − private, final and static methods and variables uses static binding.
Example of Java Static Binding
In this example, we’ve created a Calculator class having two static methods with same name but different arguments to add two and three int values respectively. In main() method, we’re calling these methods and printing the result. Based on the number of arguments passed, compiler decides the method using static binding which method is to be called and result is printed accordingly.
packagecom.tutorialspoint;classCalculator{publicstaticintadd(int a,int b){return a + b;}publicstaticintadd(int a,int b,int c){return a + b + c;}}publicclassTester{publicstaticvoidmain(String args[]){System.out.println(Calculator.add(20,40));System.out.println(Calculator.add(40,50,60));}}
Output
60
150
Java Static Binding: More Examples
Example 1
In this example, we’ve created a Calculator class having two non-static methods with same name but different arguments to add two and three int values respectively. In main() method, we’re calling these methods using object of Calculator class and printing the result. Based on the number of arguments passed, compiler decides the method using static binding which method is to be called and result is printed accordingly.
packagecom.tutorialspoint;classCalculator{publicintadd(int a,int b){return a + b;}publicintadd(int a,int b,int c){return a + b + c;}}publicclassTester{publicstaticvoidmain(String args[]){Calculator calculator =newCalculator();System.out.println(calculator.add(20,40));System.out.println(calculator.add(40,50,60));}}
Output
60
150
Example 2
In this example, we’ve created a Calculator class having two non-static methods with same name but different types of arguments to add two int values and two double values respectively. In main() method, we’re calling these methods using object of Calculator class and printing the result. Based on the type of arguments passed, compiler decides the method using static binding which method is to be called and result is printed accordingly.
packagecom.tutorialspoint;classCalculator{publicintadd(int a,int b){return a + b;}publicdoubleadd(double a,double b){return a + b;}}publicclassTester{publicstaticvoidmain(String args[]){Calculator calculator =newCalculator();System.out.println(calculator.add(20,40));System.out.println(calculator.add(20.0,40.0));}}
Output
60
60.0
Leave a Reply