Why early binding is resolved at compile time if the actual object is determined at runtime?

692 views Asked by At

I know that all objects are created at runtime when the function is called.

Binding is when we bind methods data members inside the class.

early binding is binding all the method instance variables at compile time. I thought all objects are created at runtime so it must bind also all methods data members at runtime.

Why in early binding the call to an object method is determined at compile time? if that object is created at runtime.

for example.

class A{
    public void foo(){
        //some code here
    }
}

public static void main(String[] args){
    A aInstance = new A();
    aInstance.foo();
}

foo() was resolved at compile time, but aInstance is determined at runtime.

1

There are 1 answers

0
Jirka Hanika On

This is because to bind the call means to determine the method (or function) to be called, not to determine the instance to call it on.

Early binding is preferable, because the call then executes slightly faster.

The only reason to delay the binding to the runtime might be polymorphism, where even the exact type of the object is unknown at compile type; or a simple compiler implementation that does not care about the cost of the VMT lookup.