Course contentsShow
← Java
Lesson 463 of 2,87011. Polymorphism and Dynamic DispatchFree lesson

Static vs Dynamic Binding

Distinguishing static binding for static, private, and final methods from dynamic binding for overridden instance methods.

Static vs Dynamic Binding

What you'll learn: You'll understand when Java decides method calls at compile-time versus runtime, and why certain methods behave differently from overridden instance methods.

The Core Idea

Java binds method calls to their implementations in two ways:

Static binding (early binding) happens at compile-time. The compiler knows exactly which method implementation to call based on the reference type. This applies to:

  • static methods
  • private methods
  • final methods

Dynamic binding (late binding) happens at runtime. The JVM decides which method implementation to call based on the actual object type, not the reference type. This is the mechanism behind dynamic dispatch for overridden instance methods.

Why the Difference?

Think of static binding like addressing a letter to a specific building—the destination is fixed and unchangeable. Dynamic binding is like addressing a letter to "the current resident"—the actual recipient depends on who lives there at delivery time.

Methods that cannot be overridden use static binding because there's only one possible implementation. The compiler can safely wire the call directly to that method.

Overridable instance methods use dynamic binding because a superclass reference might point to a subclass object with an overridden version. The JVM must check the actual object type at runtime to call the correct implementation—this is the vtable lookup you learned about earlier.

Example Scenarios

  • static methods: Bound to the class name at compile-time
  • private methods: Not inherited, so only one version exists
  • final methods: Cannot be overridden, so binding is fixed
  • Regular instance methods: Use dynamic dispatch via vtable

Key Takeaway: Static, private, and final methods use static binding (decided at compile-time), while overridable instance methods use dynamic binding (decided at runtime via the vtable), enabling true polymorphism.