Static vs Dynamic Binding: Compile-Time Selection and Runtime Dispatch with a Worked Java Trace

See exactly what the Java compiler selects and what the runtime receiver can change. One program, six outputs, and declared-type variations make the two stages visible.

KnowledgeGate Team

Exam prep & CS education

Updated 9 Nov 20266 min read
Static vs Dynamic Binding: Compile-Time Selection and Runtime Dispatch with a Worked Java Trace

In Printer p = new LaserPrinter(), the reference says Printer, but the object is a LaserPrinter. A learner may assume one actor, the compiler or the runtime, decides the entire call, then cannot explain the line that prints. The sharper rule is two-stage: declared types select the member and overload signature first, then the runtime object may select an override of that signature. A short runnable Java program makes both stages visible, and changing only the declared types shifts the result predictably. Working through such traces, predicting each printed line before running the code, is the everyday work of Coding & Skills.

Binding is an association made at a particular time

Binding is an association: an identifier with its declaration, a variable with a type or storage location, or a call site with an implementation. In int marks = 12;, the name and declared type are fixed before execution. Each method invocation has its own local-variable storage, and the assignment gives that invocation's variable the value 12 when the statement executes.

Binding times include language design, translation, linking or loading, and execution. Static or early binding here means the choice does not depend on the runtime receiver. Dynamic or late binding inspects runtime state, usually the receiver's class. Resolving a symbol during loading does not make a receiver-independent choice dynamic. Static binding is broader than Java's static keyword, and dynamic binding is unrelated to dynamic memory allocation. The overriding and polymorphism context behind dynamic binding is developed in Object Oriented Technology Explained.

A timeline showing which parts of a Java method call are bound at compile time versus resolved at runtime.

Static binding fixes a declaration or signature

The declared type of p is Printer, while that of seven is Integer. Thus p.label denotes Printer.label, p.mode() denotes Printer.mode(), and the visible, applicable overload is print(Number). An Integer can be passed where a Number is expected.

Overload resolution uses declared receiver and argument types, arity, and applicable conversions. Runtime does not reopen it after finding a LaserPrinter.

Construct

Java rule

Field or hidden static method

Compile-time types select it; no runtime lookup follows

Instance method call

Compile time selects its signature; runtime may select an override

Java uses these rules for the calls shown here; other languages may bind the same constructs differently.

Dynamic binding chooses an override from the runtime receiver

Overriding is a subclass supplying another implementation of the same instance-method signature. After compilation fixes print(Number), p.print(seven) inspects the receiver. Since it is a LaserPrinter, Java runs LaserPrinter.print(Number), calculates 7 x 2 = 14, and returns Laser:Number=14.

Dynamic dispatch chooses only among overrides of the compile-time-selected signature. Even though LaserPrinter defines print(Integer), the call cannot switch from print(Number) to print(Integer), because the signature was already fixed at compile time from the declared types. Base-type code can therefore obtain subclass behaviour without a type test at every call. This does not mean every method in every object-oriented language is dynamically dispatched.

Work one Java program from declarations to six outputs

Run this exact program. Calling a static method through an instance exposes hiding, though Printer.mode() and LaserPrinter.mode() are clearer production style.

java
class Printer {
    String label = "Printer";

    static String mode() {
        return "base-static";
    }

    String print(Number x) {
        return "Printer:Number=" + x.intValue();
    }
}

class LaserPrinter extends Printer {
    String label = "Laser";

    static String mode() {
        return "laser-static";
    }

    @Override
    String print(Number x) {
        return "Laser:Number=" + (x.intValue() * 2);
    }

    String print(Integer x) {
        return "Laser:Integer=" + (x + 10);
    }
}

class Demo {
    public static void main(String[] args) {
        Printer p = new LaserPrinter();
        LaserPrinter q = new LaserPrinter();
        Integer seven = Integer.valueOf(7);

        System.out.println(p.label);
        System.out.println(p.mode());
        System.out.println(p.print(seven));
        System.out.println(q.label);
        System.out.println(q.mode());
        System.out.println(q.print(seven));
    }
}

The exact output is:

Code
Printer
base-static
Laser:Number=14
Laser
laser-static
Laser:Integer=17

For p, the declared type fixes the field, static method, and print(Number) signature. Only its implementation is dynamically selected. For q, compilation sees both overloads and chooses the more specific print(Integer), which calculates 7 + 10 = 17.

Expression

Compile-time choice

Runtime choice

Result

p.label

Printer.label

none

Printer

p.mode()

Printer.mode()

none

base-static

p.print(seven)

Printer.print(Number)

LaserPrinter.print(Number)

Laser:Number=14

q.label

LaserPrinter.label

none

Laser

q.mode()

LaserPrinter.mode()

none

laser-static

q.print(seven)

LaserPrinter.print(Integer)

same method

Laser:Integer=17

A two-lane decision tree contrasting compile-time overload selection with runtime override selection for two Java method calls.

Change one declared type and predict the result

Reuse the same classes. Cover the result column and ask: which signature compiles, then which implementation runs?

Call

Compile-time signature

Runtime implementation

Result

Printer p2 = new Printer(); p2.print(seven)

print(Number)

Printer.print(Number)

Printer:Number=7

Number n = Integer.valueOf(7); q.print(n)

print(Number)

LaserPrinter.print(Number)

Laser:Number=14, from 7 x 2

q.print(seven)

print(Integer)

LaserPrinter.print(Integer)

Laser:Integer=17, from 7 + 10

((Printer) q).print(seven)

print(Number)

LaserPrinter.print(Number)

Laser:Number=14, from 7 x 2

The last cast changes the visible overloads, not the object, which remains a LaserPrinter.

Repair the common binding traps

Pair each trap with its correction:

  • Runtime object chooses the overload: the object's class seems to pick everything, but overloads are chosen at compile time from the declared reference and argument types, and the runtime only supplies the override body for that signature.

  • Fields are overridden like methods: a same-name subclass field looks overridden but is only hidden, so the reference type picks which you read, and p.label reads Printer though the object is a LaserPrinter.

  • A static method is dynamically overridden: a same-signature subclass static hides the parent's rather than overriding it, so the declared type resolves it and p.mode() stays base-static whatever the object.

  • Dynamic means any late decision: late linking or loading is not dynamic binding; a choice is dynamic only when it depends on the runtime receiver, so name the association and the runtime information it needs.

One more separation to make: binding is not the same as scope. Let global x = 10, let show() print x, and let caller() declare local x = 20 before calling show(). Under lexical or static scope, show() uses its defining context and prints 10. Dynamic scope follows the active caller and would print 20. That free-name lookup is variable resolution, not Java method dispatch. Languages set their own dispatch rules: in C++ only functions marked virtual dispatch on the runtime object, a non-virtual call uses the declared type, so the two-stage reasoning must be re-checked per language.

Exam-style questions test both decision stages

Attempt each question first, then check the answer:

  1. Predict the six printed lines, in order. Answer: Printer, base-static, Laser:Number=14, Laser, laser-static, Laser:Integer=17.

  2. In p.print(seven), which parts are decided at compile time and which at runtime? Answer: p.label, p.mode(), and the print(Number) signature are compile-time decisions; only the override that runs is selected from the runtime receiver.

  3. With Number n = Integer.valueOf(7);, what does q.print(n) return and why? Answer: the declared type Number selects print(Number); the LaserPrinter receiver dispatches to its override, giving Laser:Number=14.

Association

Expected timing

Identifier to declaration

Translation under lexical rules

Call to overload signature

Translation

Virtual call to override implementation

Runtime

Local variable to one invocation's storage

Runtime activation

A toolchain may link or load later. The core remains whether the choice depends on the runtime receiver. For the wider inheritance and polymorphism setting, OOP for Teaching CS Exams: Classes and Inheritance gives adjacent revision, and its practice questions let you rehearse the two-stage decision on fresh code.

The short rule and your next practice step

Remember three lines. Declared receiver and argument types select the member and overload signature. Fields and static methods stop there. An eligible instance call uses the runtime receiver to choose an override of that signature.

Test 11: p.print(Integer.valueOf(11)) returns Laser:Number=22 from 11 x 2; q.print(Integer.valueOf(11)) returns Laser:Integer=21 from 11 + 10. State both stages before checking. Continue with the Java course, then use the C++ programming course for comparison.