简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM