What is runtime binding?

Runtime binding is a concept that allows your application to change or extend its behavior at runtime in ways that the compiler cannot fully determine. This doesn’t mean that the compiler is no longer involved. Just that your code needs a little extra information to complete some action and that information will only be available after your application starts running.

This can give your application the ability to add new features or change existing features. Any application that allows plugins or mods will make use of runtime binding.

First of all, in order to call a method, the compiler needs to know the method signature. This lets it know what arguments need to be passed to the method and what return type to expect. All this does though is get everything setup and ready to call the method and then after the method returns, then this tells the compiler what the return type will be. Calling the method itself needs an address.

That’s it mostly. Methods are little more than a pointer to some memory that holds instructions to be run. Sure, there’s other details such as the order that the arguments should be passed to the method and if the calling code or the method should be responsible for cleaning up the stack.

But what this episode is about is that pointer. Does the compiler know where it points?

If you call a method, then as long as the compiler knows the signature, it can output the proper code needed to call the method. But there’s no guarantee that the method has its implementation in the same source file that makes the call. An application can be composed of many source files and they all get compiled individually. It’s the job of the linker to connect the dots between a method called from one compilation unit and the method body defined in another compilation unit. As long as this connection can be made by either the compiler or the linker, then the method is bound during compile time.

Listen to the full episode for more information about runtime binding. There’s another aspect of runtime binding that I’ll explain in a future episode. This is similar but has more to do with binding data than methods.