DEV Community

Sundar Joseph
Sundar Joseph

Posted on

Static vs Dynamic Binding in Java

There are certain key points that are needed to be remembered before adhering forward where we will be discussing and implementing static and dynamic bindings in Java later concluding out the differences.

private, final and static members (methods and variables) use static binding while for virtual methods (In Java methods are virtual by default) binding is done during run time based upon the run time object.
The static binding uses Type information for binding while Dynamic binding uses Objects to resolve to bind.
Overloaded methods are resolved (deciding which method to be called when there are multiple methods with the same name) using static binding while overridden methods use dynamic binding, i.e, at run time.

Static Binding :

The binding which can be resolved at compile time by the compiler is known as static or early binding. The binding of all the static, private, and final methods is done at compile-time.

example:

// Java Program to Illustrate Static Binding

// Main class
class NewClass {

// Static nested inner class
// Class 1
public static class superclass {

    // Method of inner class
    static void print()
    {

        // Print statement
        System.out.println(
            "print() in superclass is called");
    }
}

// Static nested inner class
// Class 2
public static class subclass extends superclass {

    // Method of inner class
    static void print()
    {

        // print statement
        System.out.println(
            "print() in subclass is called");
    }
}

// Method of main class
// Main driver method
public static void main(String[] args)
{

    // Creating objects of static inner classes
    // inside main() method
    superclass A = new superclass();
    superclass B = new subclass();

    // Calling method over above objects
    A.print();
    B.print();
}
Enter fullscreen mode Exit fullscreen mode

}

Output:

print() in superclass is called
print() in superclass is called

Dynamic Binding:

In Dynamic binding compiler doesn't decide the method to be called. Overriding is a perfect example of dynamic binding. In overriding both parent and child classes have the same method

example :

// Java Program to Illustrate Dynamic Binding

// Main class
public class GFG {

// Static nested inner class
// Class 1
public static class superclass {

    // Method of inner class 1
    void print()
    {

        // Print statement
        System.out.println(
            "print in superclass is called");
    }
}

// Static nested inner class
// Class 2
public static class subclass extends superclass {

    // Method of inner class 2
    @Override void print()
    {

        // Print statement
        System.out.println(
            "print in subclass is called");
    }
}

// Method inside main class
public static void main(String[] args)
{

    // Creating object of inner class 1
    // with reference to constructor of super class
    superclass A = new superclass();

    // Creating object of inner class 1
    // with reference to constructor of sub class
    superclass B = new subclass();

    // Calling print() method over above objects
    A.print();
    B.print();
}
Enter fullscreen mode Exit fullscreen mode

}

output:

print in superclass is called
print in subclass is called

Top comments (0)