Method Binding in Java

In Java, method binding refers to the process of associating a method call with its corresponding method implementation. This process can occur at either compile-time or runtime, depending on the type of binding. Java supports two types of binding: static binding and dynamic binding.


1. Static Binding (Early Binding)

Static binding occurs at compile-time and is associated with methods that are static, final, or private. These methods cannot be overridden, so the compiler determines the method to invoke based on the reference type.

Characteristics:

Example:

class Animal {
    static void sound() {
        System.out.println("Animal makes a sound");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal.sound(); // Static binding
    }
}

Here, the sound() method is resolved at compile-time because it is static.


2. Dynamic Binding (Late Binding)

Dynamic binding occurs at runtime and is associated with overridden methods in polymorphism. The JVM determines which method to invoke based on the actual object type, not the reference type.

Characteristics:

Example: