Static and Dynamic binding

Binding:

Simply binding mean linking a thing  to another thing

For example, car and engine are links together

binding in java

In java, we see binding in class and object, variable and class, member and class

For example

Class A {
Int item;
} 

Here item is an  instance variable of class A

The mean variable item is linked to class A.

Let’s understand binding with an example

public class A
{
    void m1(){
        System.out.println("hello");
    }
    
    public static void main(String arg[]){
        A ob = new A();
        ob.m1();
    }
}

In this example, we have the class A and we have a method of name m1

Then we create the object of class A  and class the method m1

Now ob.m1()

Above line On compile time
ob bind with the method of class A

Type of binding:

types of binding in java

Static binding:

        Static binding is a binding done by the compiler.

        It is also known as early binding

For Example

public class StaticBindingTest {
 
    public static void main(String args[])  {
      
       StaticBindingTest et = new StaticBindingTest();
       et.m1(5);
     
    }
   
    //overloaded method takes Collection argument
    public void m1(int i){
        System.out.println("Inside Collection sort method");
        
    }

here et object bind with m1 method it done in compile time

Dynamic binding

when we run our program at that time object bind with class is class dynamic binding

for example:

public class Test {
 
    public static void main(String args[])  {
      
       StaticBindingTest et = new Test();
       et.m1(5);
     
    }
   
    //overloaded method takes Collection argument
    public void m1(int i){
        System.out.println("Inside Collection sort method");
        
    }

In this example object et blinds with class Test at runtime .



SHARE THIS PAGE





Important Link